Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Reading through a file and checking for a specific string

by jwkrahn (Monsignor)
on Aug 19, 2013 at 23:03 UTC ( #1050097=note: print w/ replies, xml ) Need Help??


in reply to Reading through a file and checking for a specific string

use Data::Dumper; use List::Util qw(sum); # Grab text files from archive directory with glob function @files = glob ('/export/home/date_file*); $arrCount = scalar(@files); my @types = ("AB", "AC", "AD", "AE", "FG"); my @counts = ($AB, $AC, $AD, $AE, $FG); for($i = 0; $i < (@counts) ; $i++ ) { @counts[$i] = 0; } for($i = 0; $i < $arrCount; $i++) { $file = @files[$i]; open(FILE, $file) or die "Can't open `$file': $!"; @lines = <FILE>; close FILE; foreach $line (@lines) { $str = $line; $var = substr($str, 41, 2); for( $i=0; $i<(@types); $i++ ) { if ( $var eq "@types[$i]" ){ @counts[$i]++; } } } } my $sum = 0; for ( @counts) { $sum += $_; } for( $j=0; $j<(@types); $j++ ) { print "@types[$j]\t: @sums[$j] \n"; $j = $j + 1; } print "Total \t: $sum";
#!/usr/bin/perl use warnings; use strict; use List::Util qw(sum); # Grab text files from archive directory with glob function @ARGV = glob '/export/home/date_file*'; my %counts = ( AB => 0, AC => 0, AD => 0, AE => 0, FG => 0, ); while ( my $line = <> ) { my $var = substr $line, 41, 2; $counts{ $var }++ if exists $counts{ $var }; } my $sum = sum values %counts; for my $type ( keys %counts ) { print "$type\t: $counts{$type}\n"; } print "Total \t: $sum";


Comment on Re: Reading through a file and checking for a specific string
Select or Download Code
Re^2: Reading through a file and checking for a specific string
by Monk::Thomas (Beadle) on Aug 20, 2013 at 09:01 UTC
    Nice rewrite. Although the connection between
    @ARGV = glob '/export/home/date_file*';
    and
    while ( my $line = <> ) {
    is not exactly obvious and could lead to some serious head-scratching. Especially if the program grows a bit further and actually uses command line arguments. Same as above, with explicit file open / close.
    #!/usr/bin/perl use warnings; use strict; use List::Util qw(sum); # Grab text files from archive directory with glob function my @files = glob '/export/home/date_file*'; my %counts = ( AB => 0, AC => 0, AD => 0, AE => 0, FG => 0, ); for my $file (@files) { open my $fh, '<', $file or die "$file: $!"; while ( my $line = <$fh> ) { my $var = substr $line, 41, 2; $counts{ $var }++ if exists $counts{ $var }; } close $fh or die "$file: $!"; } my $sum = sum values %counts; for my $type ( keys %counts ) { print "$type\t: $counts{$type}\n"; } print "Total \t: $sum\n";
      Thanks! This works flawlessly.

      I just had one more question. How do I avoid printing out values that are not found? So currently it prints out all 5 even if one of the match is not found.
      AB : 135172
      FG : 248782
      AD : 64
      AE : 0
      AC : 0

      I would like to avoid AE and AC in this case since they are not found.

      Also, two more requirements just came up. There is a one word description attached to each string I am supposed to find and they are supposed to be printed alphabetically.

      Would I need to make a separate array to print out description next to each string I am printing? This is what the result should look like:
      AB(ABYUSID) : 135172
      FG(FGIUIO) : 248782
      AD(ADHGUT) : 64
      AE(AERUTOT) : 0
      AC(ACVHGTI) : 0
      Would I need to store these one word descriptions in a separate array and print from there? I tried doing it that way but then it becomes infinite loop and keeps printing these values every time they are found
      Thanks for all the help!

        Store the description in a hash not an array.

        #!/usr/bin/perl use strict; # test data my %counts = ( AB => 135172, AC => 0, AD => 64, AE => 0, FG => 248782, ); my %descr = ( AB => 'ABYUSID', FG => 'FGIUIO', AD => 'ADHGUT', AE => 'AERUTOT', AC => 'ACVHGTI', ); for my $type ( sort keys %counts ) { # add sort next if ($counts{$type} == 0); # skip zeros print "$type($descr{$type})\t: $counts{$type}\n"; # add descr }
        poj
        While it is possible to skip the zero matches, however, it may actually better to include them.
        That way you distinguish between "searched but not found" and "not searched at all".
        Thanks poj! It works. I modified it according to how I needed it but thanks for your help! I appreciate it
        Do you know if there is a way to print out the two letter string I am looking for if it doesn't exist in the file? So for example, if I have another string in file "FD" that doesn't match anything, could I still print it out just so I know which ones I am missing. Thanks!
      Do you know if there is a way to print out the two letter string I am looking for if it doesn't exist in the file? So for example, if I have another string in file "FD" that doesn't match anything, could I still print it out just so I know which ones I am missing. Thanks!
        NVM found a workaround.
Re^2: Reading through a file and checking for a specific string
by vihar (Acolyte) on Aug 20, 2013 at 13:36 UTC
    Thanks for your suggestion guys!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1050097]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2014-07-25 02:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (167 votes), past polls