Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by vihar (Acolyte)
on Aug 20, 2013 at 13:35 UTC ( #1050184=note: print w/ replies, xml ) Need Help??


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

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!


Comment on Re^3: Reading through a file and checking for a specific string
Re^4: Reading through a file and checking for a specific string
by poj (Curate) on Aug 20, 2013 at 17:41 UTC

    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
Re^4: Reading through a file and checking for a specific string
by soonix (Deacon) on Aug 21, 2013 at 11:43 UTC
    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".
Re^4: Reading through a file and checking for a specific string
by vihar (Acolyte) on Aug 21, 2013 at 14:02 UTC
    Thanks poj! It works. I modified it according to how I needed it but thanks for your help! I appreciate it
Re^4: Reading through a file and checking for a specific string
by vihar (Acolyte) on Aug 22, 2013 at 15:49 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!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1050184]
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-28 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (198 votes), past polls