in reply to Re: Extracting elements from array
in thread Extracting elements from array

When i read the title i was sure grep was the solution.. when i read all solutions used map i was surprised.

Using grep was trickier than i thought (but is a function i started using recently (2 years or so)) was trickier 'cause of his return value accomplished using $_ = ..
use strict; use warnings; use Data::Dump; my $aref =[ { val => 'action', acc => 'Accounts', id => 'None', }, { name => 'PA1', id => 'AUTOB' }, { name => undef, id => 'AUTOP' }, { name => '0', id => 'BOP' }, { name => 0, id => 'DFIRE' }, { name => '', id => 'HOME' }, { name => 'CM7', id => 'CUMBR' }, { name => 'BR1', id => 'PUMBR' }, { name => 'TY6', id => 'ECUST' }, ]; dd [ grep { exists $_->{name} and $_={'name' => $_->{name}} } @$aref ] +; # OUTPUT [ { name => "PA1" }, { name => undef }, { name => 0 }, { name => 0 }, { name => "" }, { name => "CM7" }, { name => "BR1" }, { name => "TY6" }, ]


HtH
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^3: Extracting elements from array -- grep
by AnomalousMonk (Archbishop) on Jan 15, 2016 at 16:37 UTC
    [ grep { exists $_->{name} and $_={'name' => $_->{name}} } @$aref ]

    Beware that  $_={'name' => $_->{name}} clobbers  $aref because  $_ is an alias. (Of course,  $_ in map is also an alias, so it's possible to do oneself an injury with map also, but also easier to avoid. :-)


    Give a man a fish:  <%-{-{-{-<