in reply to IF and GREP Question

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

Replies are listed 'Best First'.
Re^2: IF and GREP Question
by NetWallah (Canon) on Sep 18, 2013 at 03:04 UTC
    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

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

    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.

        "if ()" imposes a scalar context.

        Actually it imposes a boolean context, which is basically the same as scalar context.

        "if (@found)" sees undef or zero

        An array in boolean context cannot be undef.    In fact, defined doesn't work with arrays anymore.