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

Hi experts,

I have a perl program, wherein i have declared array variable to match the regex and get the desired value but it working locally in strawbery perl but not in the server. can you please check and advise.

open(QUEUES, $queuecmd); my @lines=<QUEUES>; close QUEUES; my @queues = grep {s/.*count= *(\d+),.* Queue=or_event_queue\s*/$1/} @ +lines; print "$queues[0]\n";

In the above program @queues did not get the correct value. Below is the $queuecmd command output read in to @lines

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

@queues and $queues[0] should have value 3079

Thanks and regards,

Discipulus added code tags where needed

Replies are listed 'Best First'.
Re: Did not get the regex matched value in perl array variable
by choroba (Cardinal) on Dec 27, 2017 at 08:16 UTC
    You should always check the return value of open.
    open QUEUES, $queuecmd or die "$queuecmd: $!";

    If you're running the script on the server through CGI, the error message will be located in the error log.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Did not get the regex matched value in perl array variable
by thanos1983 (Parson) on Dec 27, 2017 at 09:41 UTC

    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.

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

    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:

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

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

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      ++, 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!
Re: Did not get the regex matched value in perl array variable
by Marshall (Canon) on Dec 28, 2017 at 00:54 UTC
    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
      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>;