Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^4: filtering an array

by prbndr (Acolyte)
on Sep 01, 2012 at 22:48 UTC ( #991208=note: print w/ replies, xml ) Need Help??


in reply to Re^3: filtering an array
in thread filtering an array

i see. so if my array @positions is already defined and created, do i need to loop over all the values in @positions and then execute the code you posted?

something like:

 for (@positions) {@positions = grep $_->[1] =~ /ac/i, @positions;}

and then:

 for (@positions) {@positions = map $_->[0], @positions;}


Comment on Re^4: filtering an array
Select or Download Code
Re^5: filtering an array
by philiprbrenan (Monk) on Sep 01, 2012 at 22:59 UTC

    You do not need the for becuase grep loops over the array you give it. Please change:

    for (@positions) {@positions = grep $_->[1] =~ /ac/i, @positions;}

    to:

    @positions = grep $_->[1] =~ /ac/i, @positions;
      right, but the error still persists. what here isn't defined?
Re^5: filtering an array
by philiprbrenan (Monk) on Sep 01, 2012 at 23:03 UTC

    Likewise, the for is unnecessary in this code too:

    for (@positions) {@positions = map $_->[0], @positions;}

    It should just be:

    @positions = map $_->[0], @positions;

    because map iterates over the array you give it. Even better, you could write:

    $_ = $_->[0] for @positions;

    here we are making the for implicit in map explicit.

      perl still has a problem with this:

      @positions = grep $_->[1] =~ /ac/i, @positions;

      with the same error Use of uninitialized value in pattern match (m//)

        One or more of the elements of the array @positions is undefined. The easiest way to check this is like this:

        use Data::Dump qw(pp); pp([@positions]);

        This will give you a printout of the value of each element in array @positions. I expect we will see that some are "undef" meaning undefined. It will be these elements that are causing the problem.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991208]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2014-07-31 12:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls