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!

In reply to Re: Did not get the regex matched value in perl array variable by thanos1983
in thread Did not get the regex matched value in perl array variable by sreek3502

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.