in reply to Did not get the regex matched value in perl array variable

I would not use grep in this way.
Better would be to process the input lines one by one and push a result if there is one. The code my @lines=<QUEUES>; and the grep code are both "foreach loops". Only one loop over the input lines is needed.
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @queue; while (<DATA>) { push @queue,$1 if (/count=\s*(\d+)/ and $1 != 0); } print Dumper \@queue; =prints: $VAR1 = [ '3079' ]; =cut __DATA__ Host 'server02' connected ============================================================ Hostname: 'server02' count= 0, delivering= 0, Queue=opr_action_launch_queue count= 0, delivering= 0, Queue=queue/alert_engine_notificat +ion count= 3079, delivering= 0, Queue=or_event_queue count= 0, delivering= 0, Queue=recipient_notification count= 0, delivering= 0, Queue=queue/alert_engine_alert count= 0, delivering= 0, Queue=failed_recipient_notificatio +n

Replies are listed 'Best First'.
Re^2: Did not get the regex matched value in perl array variable
by Laurent_R (Canon) on Dec 28, 2017 at 13:39 UTC
    Yes, Marshall, you're right that storing the content of the file in an array and then grepping the array essentially means looping twice over the data (which can be inefficient with large data). The problem is not so much due to grep, however, but to the use of a temporary array to store the contents of the file.

    You could use grep without incurring the cost of two implicit loops by removing the use of a temporary array:

    my @queue = grep { /count=\s*(\d+)/; $1 } <DATA>;