Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Assign regexp output to array not working

by bowei_99 (Friar)
on Aug 09, 2008 at 23:28 UTC ( [id://703347]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to parse a line once to get what I need, so I do that with a regexp and try dumping to an array. But when I do, I get:
Array found where operator expected at check.pl line 82, at end of lin +e (Missing operator before ?)
The offending line 82 is the assignment of the regexp output below:
if (@data = ($line =~ STATUS_COMPLETED_RE )) { $stats{level} = $shift @data; $stats{group} = $shift @data; $stats{total} = $shift @data; }
where the regexp is:
use constant STATUS_COMPLETED_RE => qr{ ^string-here \s string-here: \s \((\w+)\) \s #alarm level ([\w|\s]+) \s completed, \s #job completed Total\s (\d+) \sclient\(s\), \s #total client +s ( #Details - (\d+) \s # number ([\w|\s\(\)]+) ,|\. # su +cceed,fail,etc. )* # any numbe +r of }xms ;
The code works if I don't output to an array, but since I don't know big the resulting match list from the regexp will be, I'd prefer to dump it to an array and process that array. In other words, the Details section from the regexp could be large, and I'm trying to make it robust.


Update Thanks, yeah, I meant shift and not $shift. Knew it was something trivial. Works now. Anyone see what I'm missing?

-- Burvil

Replies are listed 'Best First'.
Re: Assign regexp output to array not working
by betterworld (Curate) on Aug 09, 2008 at 23:52 UTC

    I think you wanted to write shift without a dollar sign. Are you using strict and warnings?

    Yet even without strict, the error message is clear: Array found where operator expected means that perl thinks you want to operate on the scalar $shift but instead of an operator, it finds the array you wanted to shift.

Re: Assign regexp output to array not working
by Cristoforo (Curate) on Aug 10, 2008 at 02:40 UTC
    I tried running your regex on 2 different lines. The first sample line failed and the second one passed (although its not producing the results I think you want).
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use constant STATUS_COMPLETED_RE => qr{ ^string-here \s string-here: \s \((\w+)\) \s #alarm level ([\w|\s]+) \s completed, \s #job completed Total\s (\d+) \sclient\(s\), \s #total client +s ( #Details - (\d+) \s # number ([\w|\s\(\)]+) ,|\. # su +cceed,fail,etc. )* # any numbe +r of }xms ; my @sample_str = ('string-here string-here: (alarm_level_5) group5 com +pleted, Total 55 client(s),', 'string-here string-here: (alarm_level_2) group2 completed, To +tal 22 client(s), 422 succeed,622 fail,911 no_pass.'); my (@data, %stats); for my $line ( @sample_str ) { if (@data = ($line =~ STATUS_COMPLETED_RE )) { print Data::Dumper->Dump([\@data], [qw/*data/]); $stats{level} = shift @data; $stats{group} = shift @data; $stats{total} = shift @data; } else { print "No match\n"; } } print Data::Dumper->Dump([\%stats], [qw/*stats/]);
    Output from running the program was:
    C:\perlp>perl try.pl No match @data = ( 'alarm_level_2', 'group2', '22', '622 fail,', '622', 'fail' ); %stats = ( 'group' => 'group2', 'level' => 'alarm_level_2', 'total' => '22' ); C:\perlp>C:\perlp>
    The first sample failed because of line:
    Total\s (\d+) \sclient\(s\), \s #total client
    The \s requires a space even if there isn't a details section that follows. It could be fixed with \s?.

    In parsing the details section, notice from the output that @data, only picked up the second detail set.

    It did this because it only captures the last successful match. First, it captured 422 succeed,, then 622 fail, and failed to match 922 no_pass (because the line doesn't end in a comma).

    Also, it's capturing the whole detail section and the separate 2 items inside the details. I think what you mean (?) is to capture all the detail groups as a whole (i.e., 422, 622 and 911 and their status).

    If thats what you mean, then this might be the code:

Re: Assign regexp output to array not working
by GrandFather (Saint) on Aug 09, 2008 at 23:54 UTC

    Not so much what you are missing, as what is extra. What is $shift? Do you perhaps mean shift?


    Perl reduces RSI - it saves typing
Re: Assign regexp output to array not working
by converter (Priest) on Aug 10, 2008 at 00:26 UTC
    Are you running the code you listed in a larger context? If so, the real error could be before line 82. Have you tried reducing the code to the minimum required to reproduce the problem?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://703347]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 04:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found