Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

IF and GREP Question

by Anonymous Monk
on Sep 18, 2013 at 02:20 UTC ( [id://1054572]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I am new to Perl and have a question regarding the grep function and the if control structure. I have the following if/grep structure.

if (grep {/$backup_job/} <NB_FILE>) { print "backup job found for $backup_job\n"; } else { print "$backup_job does not have a backup job !!\n"; }

The test results are correct:

./test.pl test.txt :1: puccini backup job found for puccini :2: fakeserver fakeserver does not have a backup job !! :3: cgndsdr4 backup job found for cgndsdr4

I am trying to display the actual data instead of the print statements when backup jobs are found and the print statement if a backup job was not found. Like the following:

./test.pl test.txt :1: puccini NB_ISA_SNOH_puccini_DB_diff NB_ISA_SNOH_puccini_DB_full :2: fakeserver fakeserver does not have a backup job !! :3: cgndsdr4 NB_toi_tko_cgndsdr4_fl

Thank you again in advance!

Replies are listed 'Best First'.
Re: IF and GREP Question
by kcott (Archbishop) on Sep 18, 2013 at 02:44 UTC

    I don't know how you're generating some of that output (e.g. :1:, :2:, etc.); however, assuming $backup_job is being set in some sort of loop, something like this will probably do what you want:

    for my $backup_job (@backup_jobs) { ... my @found = grep {/$backup_job/} <NB_FILE>; if (@found) { print "$_\n" for @found; } else { print "$backup_job does not have a backup job !!\n"; } ... }

    -- Ken

      That will not work because the NB_FILE handle will exhaust the records in the first loop.

      Depending on how many records you expect, you may need to save the contents of the file in an array, to avoid the I/O overhead.

      If that is not viable, read the file, record-at-a-time in a loop, then loop through @backup_jobs for each record.

                   My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

        "That will not work because the NB_FILE handle will exhaust the records in the first loop."

        The OP has not provided sufficient information for you to make that statement.

        I stated what I didn't know as well as the assumptions I had made.

        For all you or I know, that first "..." contains an open statement which reuses the NB_FILE filehandle. The for loop was a stated assumption; perhaps the code doesn't look like that at all; perhaps it's in a subroutine which opens the NB_FILE filehandle every time it's called.

        Anyway, the point is now moot: the OP's subsequent response ("Thank you sir, the code worked great!") rather suggests you're wrong.

        -- Ken

      Thank you sir, the code worked great! If I could ask, I understand placing the grep results into the array. Could you explain the "if (@found)" and the next line of code ?

        "if ()" imposes a scalar context.

        An array in a scalar context returns the number of elements.

        If the array is empty, "if (@found)" sees undef or zero , which is FALSE in perl.

        If the array has values, "if(@found)" sees a non-zero value, which is TRUE in perl.

                     My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

Re: IF and GREP Question
by Anonymous Monk on Sep 18, 2013 at 03:21 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1054572]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-03-28 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found