in reply to Extracting elements from array
Without the need to "manually" step over the zeroth element (i.e., process only hash refs. that have a 'name' key):
<c> c:\@Work\Perl>perl -wMstrict -le "use Data::Dump qw(dd); ;; my $data = [ { 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' }, ]; ;; my $k = 'name'; my $names = [ map { exists $_->{$k} ? { $k => $_->{$k} } : () } @$data ]; dd $names; " [ { name => "PA1" }, { name => undef }, { name => 0 }, { name => 0 }, { name => "" }, { name => "CM7" }, { name => "BR1" }, { name => "TY6" }, ]
Update: Changed example code to include various false values for $k selected key.
Give a man a fish: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Extracting elements from array -- grep
by Discipulus (Canon) on Jan 15, 2016 at 08:44 UTC | |
by AnomalousMonk (Archbishop) on Jan 15, 2016 at 16:37 UTC |