Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Do you know where your variables are?
 
PerlMonks  

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

by Monk::Thomas (Sexton)
on Aug 20, 2013 at 09:01 UTC ( #1050153=note: print w/ replies, xml ) Need Help??


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

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";


Comment on Re^2: Reading through a file and checking for a specific string
Select or Download Code
Re^3: Reading through a file and checking for a specific string
by vihar (Acolyte) on Aug 20, 2013 at 13:35 UTC
    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!
Re^3: Reading through a file and checking for a specific string
by vihar (Acolyte) on Aug 22, 2013 at 15:57 UTC
    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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2014-04-21 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (495 votes), past polls