#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Beginner in need of your assistance


    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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    Read each of the lines in your file, chomp your line, store the variable as a key in a hash if it does not exist yet, with 1 as the value for it, or increment the value if it does exist. At the end, print your hash.

    This looks too much like a school assignment for me to give you the five lines of code that will do it.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by Laurent_R
    Read each of the lines in your file, chomp your line, store the variable as a key in a hash if it does not exist yet, with 1 as the value for it, or increment the value if it does exist. At the end, print your hash.

    This looks too much like a school assignment for me to give you the five lines of code that will do it.
    i do go to school but not for programming, perl is just a hobby. if i understand what youre saying wouldnt that just give me a hash with x = 5 and o = 2? such as...

    Code:
    foreach $a ( @array ) {
         $seen{"$a"} += 1     # no need to declare it to 0 if this is what you were saying
    }
    looking for how many times each pattern was seen, so even if i put the whole file into a split array
    Code:
    if ( /(.*).*?\1/ ) {
    }
    that would only give me one iteration of the first possible match...just cant think of a way to find the rest...
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    so after some tinkering i got it to one line i need fixing..have tried:
    Code:
    	if ( "$m" =~ ( /"$patternx"/ x $count ) ) {}
            # and 
            if ( ( "$m" eq "$patternx" ) x $count ) {}
    full code:
    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";
    }
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    Judging from the example in your original post, all you need is to check how many times each line is in the file. So, just read the file and store the lines in the hash with a counter, and then print the counter. So, something like this should do it:

    Perl Code:
    my $in_file = "./file.txt";
    my %seen;
    open my $IN_FH, "<", $in_file or die "can't open $in_file $! \n";
    while (my $line = <$IN_FH>) {
         chomp $line;
         $seen{$line} ++;
    }
    print "$_: seen $seen{$_} times \n" foreach keys %seen;
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    ty Lauren, I have not as of yet tried your example but the issue was resolved at:
    http://www.perlguru.com/gforum.cgi?post=73215;sb=post_latest_reply;so=ASC;forum_view=forum_view_collapsed;guest=7899500

    ...for anyone having a similar issue

IMN logo majestic logo threadwatch logo seochat tools logo