CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
pattern matching (not expressions)

 



omega
Novice

Dec 28, 2012, 5:24 PM

Post #1 of 5 (1565 views)
pattern matching (not expressions) Can't Post

 
im trying to create a program that will match any number of possible patterns and print the amount of times that pattern was seen given a file of variables with each variable on a separate line.

such as...
x
o
x
x
o
x

would see x 5 times, o 2 times, xo 2 times, xox 2 times, and xoxx 1 time(and any other possible combination you can think of).

i apologize if this is a duplicate thread, i have googled the hell outa this and came up w nothing.


omega
Novice

Dec 28, 2012, 6:09 PM

Post #2 of 5 (1564 views)
Re: [omega] pattern matching (not expressions) [In reply to] Can't Post

so after some tinkering i got it to a one line i need fixing...with your help ;)


Code
#!/usr/bin/perl 

use 5.010;
use warnings;

open DD, '<', 'file.txt';
while ( <DD> ) {
chomp;
if ( /(\w+)/ ) {
$pattern .= "_$1";
}
$matches{"$pattern"} = 1;
}
close DD;

open DDX, '<', 'file.txt';
while ( <DDX> ) {
chomp;
if ( /(\w+)/ ) {
$patternx .= "_$1";
}

foreach $m ( keys %matches ) {
$count = 1;
while ( $count > 0 ) {
if ( ( "$m" =~ /"$patternx"/ ) x $count ) { # expression thats failing
$count += 1;
} else {
$matched{"$m"} = $count;
$count = 0;
}
}
}
}
close DDX;

foreach ( sort keys %matched ) {
print "$matched{$_}: $_\n";
}

[\code]


(This post was edited by omega on Dec 28, 2012, 6:45 PM)


BillKSmith
Veteran

Dec 29, 2012, 8:49 AM

Post #3 of 5 (1551 views)
Re: [omega] pattern matching (not expressions) [In reply to] Can't Post

I find it much clearer to extract the strings rather than build them.

Code
use strict; 
use warnings;
use List::Util qw(sum);

my @raw_text = <DATA>;
s/^(.).*/$1/ms foreach @raw_text;
my $txt = join q(), @raw_text;

my %seen;
for my $length (1 .. length $txt) {
for my $i (0 .. length($txt) - $length) {
my $string = substr($txt, $i, $length);
$seen{$string}++;
}
}
die "Intenal Error\n" if sum( values %seen ) != triangle(length($txt));

foreach my $string (sort {$seen{$b} <=> $seen{$a} } keys %seen) {
printf "%-12s %2u \n", $string, $seen{$string}
}

sub triangle {
my ($n) = @_;
return $n * ($n +1) /2; # One definition of triangle numbers;
}
__DATA__
x
o
x
x
o
x

Good Luck,
Bill


omega
Novice

Dec 29, 2012, 10:20 AM

Post #4 of 5 (1546 views)
Re: [BillKSmith] pattern matching (not expressions) [In reply to] Can't Post

wow, id be lying if i didnt say modules and internal file-handles werent above my head. but yes that code is exactly what i was trying to accomplish, i thank you very much.


Rahul6990
Novice

Jan 2, 2013, 11:10 AM

Post #5 of 5 (1450 views)
Re: [omega] pattern matching (not expressions) [In reply to] Can't Post

Same Problem.

http://perlguru.com/gforum.cgi?post=73239;sb=post_latest_reply;so=ASC;forum_view=forum_view_collapsed;;page=unread#unread

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives