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

Hello sreek3502,

Welcome to the Monastery. We do not have an update on your question if you manage to resolved your problem based on the proposed solution of fellow Monk Corion. Well I hope that you managed to resolved it, alternatively update your question with more data so we can assist you more.

A few points that I would like to add here as I read your code. Always but always use strict and warnings. Maybe you are using them in your script but since I do not see them I assume that you do not. Second point to mention, always use lexical file handles, why? There is a really nice article Don't Open Files in the old way that explains the reasons. Third point to mention, when you reading a file either line by line or into an array 99.9% you will need to remove new line characters. Perl come with a handy and easy to use function chomp. Last point to mention here, is how to view complex data structures for debugging purposes, you can use Data::Dumper a core module that is very very useful.

Having said all of that sample of your script based on the proposed modifications and output, assuming that the file '$queuecmd' contains the DATA that you demonstrate on your code sample.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use feature 'say'; my $queuecmd = 'queues.txt'; open(my $fh, '<', $queuecmd) or die "Could not open: '".$queuecmd."' $!"; chomp(my @lines= <$fh>); close $fh or warn "Could not close: '".$queuecmd."' $!"; print Dumper \@lines; my @queues = grep {s/.*count= *(\d+),.* Queue=or_event_queue\s*/$1/} @ +lines; say $queues[0]; __END__ $ perl test.pl $VAR1 = [ 'Host \'server02\' connected', '=========================================================== +=', 'Hostname: \'server02\'', ' count= 0, delivering= 0, Queue=opr_action_launch +_queue', ' count= 0, delivering= 0, Queue=queue/alert_engin +e_notification', ' count= 3079, delivering= 0, Queue=or_event_queue', ' count= 0, delivering= 0, Queue=recipient_notific +ation', ' count= 0, delivering= 0, Queue=queue/alert_engin +e_alert', ' count= 0, delivering= 0, Queue=failed_recipient_ +notification' ]; 3079

Update: Fellow Monk soonix pointed something that I skipped completely. Find bellow the updated answer to your question:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use feature 'say'; my $queuecmd = 'ls -la'; open(my $cm, '-|', $queuecmd) or die "Could not open pipe command: '".$queuecmd."' $!"; chomp(my @lines= <$cm>); close $cm or die "Could not close pipe command: '".$queuecmd."' $!"; print Dumper \@lines; my @queues = grep {s/.*count= *(\d+),.* Queue=or_event_queue\s*/$1/} @ +lines; say $queues[0] if $queues[0]; __END__ $ perl test.pl $VAR1 = [ 'total 20', 'drwxr-xr-x 2 tinyos tinyos 4096 Dec 27 14:38 .', 'drwxr-xr-x 5 tinyos tinyos 4096 Dec 27 14:37 ..', '-rw-r--r-- 1 tinyos tinyos 987 Dec 27 14:37 module.pl', '-rw-r--r-- 1 tinyos tinyos 1109 Dec 27 14:38 test.pl', '-rw-r--r-- 1 tinyos tinyos 1111 Dec 27 14:37 test.pl~' ];

You can also use many modules that will process the file for you in a more efficient way if you desire. One of the available modules IO::All. Sample of code bellow:

#!/usr/bin/perl use strict; use IO::All; use warnings; use Data::Dumper; use feature 'say'; my $queuecmd = 'queues.txt'; my @lines = io($queuecmd)->chomp->slurp; # Chomp as you slurp print Dumper \@lines; my @queues = grep {s/.*count= *(\d+),.* Queue=or_event_queue\s*/$1/} @ +lines; say $queues[0]; __END__ $ perl module.pl $VAR1 = [ 'Host \'server02\' connected', '=========================================================== +=', 'Hostname: \'server02\'', ' count= 0, delivering= 0, Queue=opr_action_launch +_queue', ' count= 0, delivering= 0, Queue=queue/alert_engin +e_notification', ' count= 3079, delivering= 0, Queue=or_event_queue', ' count= 0, delivering= 0, Queue=recipient_notific +ation', ' count= 0, delivering= 0, Queue=queue/alert_engin +e_alert', ' count= 0, delivering= 0, Queue=failed_recipient_ +notification' ]; 3079

Update for module: Following update for the use of module.

#!/usr/bin/perl use strict; use IO::All; use warnings; use Data::Dumper; use feature 'say'; my $queuecmd = 'ls -la'; my $io = io->pipe($queuecmd)->chomp; my @queues; while ( defined(my $queue = $io->getline) ) { push @queues, $queue if (index($queue, 818) != -1); # instead of g +rep or $queue =~ m/pattern/ } print Dumper \@queues; say $queues[0]; # my @queues = grep {s/.*count= *(\d+),.* Queue=or_event_queue\s*/$1/} + @lines; # say $queues[0]; __END__ $ perl module.pl $VAR1 = [ '-rw-r--r-- 1 tinyos tinyos 818 Dec 27 14:38 test.pl' ]; -rw-r--r-- 1 tinyos tinyos 818 Dec 27 14:38 test.pl

Update 2 for module: In case you want to use array and grep with the use of a module.

#!/usr/bin/perl use strict; use IO::All; use warnings; use Data::Dumper; use feature 'say'; my $queuecmd = 'ls -la'; my @lines = io->pipe($queuecmd)->chomp->slurp; print Dumper \@lines; my @queues = grep {s/.*count= *(\d+),.* Queue=or_event_queue\s*/$1/} @ +lines; say $queues[0] if $queues[0]; __END__ $ perl module.pl $VAR1 = [ 'total 24', 'drwxr-xr-x 2 tinyos tinyos 4096 Dec 27 17:12 .', 'drwxr-xr-x 5 tinyos tinyos 4096 Dec 27 14:37 ..', '-rw-r--r-- 1 tinyos tinyos 462 Dec 27 17:12 module.pl', '-rw-r--r-- 1 tinyos tinyos 585 Dec 27 15:04 module.pl~', '-rw-r--r-- 1 tinyos tinyos 818 Dec 27 14:38 test.pl', '-rw-r--r-- 1 tinyos tinyos 1111 Dec 27 14:37 test.pl~' ];

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: Did not get the regex matched value in perl array variable
by soonix (Chancellor) on Dec 27, 2017 at 13:01 UTC
    ++, but more likely $queuecmd is not a file name, but a command, in which case an open mode of '-|' would be appropriate. For testing then
    my $queuecmd = 'cat queues.txt';
    ,-)

      Hello soonix,

      ++, you are absolutely right, I have updated my answer. Thanks for pointing out my mistake. :)

      BR / Thanos

      Seeking for Perl wisdom...on the process of learning...not there...yet!