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

Hello Monks,

I am trying to use perl to extract and parse the event logs on my XP box. An array (called filter) is to hold events that I might want to parse for. For instance,
push @filter, [("EventLog", "started", "The EventLog has been Started +")]; push @filter, [("EventLog", "stopped", "The EventLog has been Stopped" +)];
i'm getting problems poping the arrays.
@singlefilter = pop(@filter)
The only thing that works is:
$logtype = {@singlefilter[0]}[0]; $Regex_Event = {@singlefilter[0]}[1]; $Friendly_Output = {@singlefilter[0]}[2];
I don't understand though why I can't just use:
$logtype=@singlefilter[0];
etc.. Can anyone enlighten me?

Replies are listed 'Best First'.
Re: Popping Arrays from an Arrays of Arrays
by Limbic~Region (Chancellor) on Mar 30, 2004 at 15:05 UTC
    iana,
    Ok - first a matter of style, I prefer:
    my $logtype = $singlefilter[0]->[0]; # or my $Regex_Event = $singlefilter[0][1];
    Back to your problem with pop. You are removing an array reference and assigning it to an array - you need to dereference at the same time:
    my @singlefilter = @{ pop @filter }; my $logtype = $singlefilter[0];
    Cheers - L~R
Re: Popping Arrays from an Arrays of Arrays
by Tomte (Priest) on Mar 30, 2004 at 15:07 UTC

    The crux is the line

    @singlefilter = pop(@filter)
    You create a new array containing on element, the reference to the poped array.

    The line should read

    @singlefilter = @{pop(@filter)}
    because you push references to arrays not arrays onto @filter (explicitly by using []) during the push, creating references to anonymous arays), and you couldn't do it any other way, because an array can only hold scalar values; a so called AoA is in reality an Array of Array-references!

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

Re: Popping Arrays from an Arrays of Arrays
by Roy Johnson (Monsignor) on Mar 30, 2004 at 15:18 UTC
    The things that you want to work point out some muddiness in your understanding of Perl data types. It would be worthwhile for you to train yourself to pay attention to the sigils (type indicators). For example, you wanted to do:
    $logtype=@singlefilter[0];
    Obviously, the left side is a scalar variable, while the right side is an array slice. That's not a good match. Just for typing reasons, it would be better to have
    $logtype=$singlefilter[0];
    so you're assigning scalar to scalar. Of course, that still won't give you what you want, because you don't want the reference to the array, you want something out of the referenced array:
    $logtype = ${$singlefilter}[0]; # is the same as $logtype = $singlefilter[0]->[0]; # is the same as $logtype=$singlefilter[0][0];
    But really, you want to assign several pieces out of the referenced array, so you can go back to using an array slice, assigning to a list:
    ($logtype, $Regex_Event, $Friendly_Output) = @{$singlefilter[0]}[0,1,2 +];
    The usual perldocs are recommended:
    perldoc perldata perldoc perlreftut perldoc perllol perldoc perlref

    The PerlMonk tr/// Advocate
Re: Popping Arrays from an Arrays of Arrays
by iana (Initiate) on Mar 30, 2004 at 16:30 UTC
    Thankyou all. I understand now.