Re: Extracting elements from array
by toolic (Bishop) on Jan 14, 2016 at 21:19 UTC
|
use warnings;
use strict;
use Data::Dumper;
my $data =
[
{
val => "action",
acc => "Accounts",
id => "None",
},
{ name => "PA1", id => "AUTOB" },
{ name => "PB3", id => "AUTOP" },
{ name => "BOX", id => "BOP" },
{ name => "DW0", id => "DFIRE" },
{ name => "QW3", id => "HOME" },
{ name => "CM7", id => "CUMBR" },
{ name => "BR1", id => "PUMBR" },
{ name => "TY6", id => "ECUST" },
]
;
my @all_names = map { $_->{name} } @{ $data }[1 .. $#$data];
print Dumper(\@all_names);
__END__
$VAR1 = [
'PA1',
'PB3',
'BOX',
'DW0',
'QW3',
'CM7',
'BR1',
'TY6'
];
| [reply] [d/l] |
|
|
I explained it wrong I guess, but using map is cool. The problem is that I also need the data as this in the end:
[
{name => "PA1"},
{name => "PB3"},...
]
Thanks where I am stuck, thanks! | [reply] [d/l] |
|
|
.. also need ..
my @hash_names = map{ {name=>$_} } @all_names;
poj
| [reply] [d/l] |
|
|
my @all_names = map { {name => $_->{name}} } @{ $data }[1 .. $#$data];
| [reply] [d/l] |
|
|
|
|
|
|
|
|
The point to map is that you can run the elements of the list through any code you like in the map block.
map assigns the result of the last evaluated statement in the block, you don't always have to can't say return $foo;, but can just say $foo, as shown in all these examples. I tend to eschew many of Perl's shortcuts (like omitting a return statement from a block) because I prefer readability, but when it's a single statement, as is often the case with map, it's nice to be able to skip it.
But if it's clearer to you to write:
my @hash_names = map{ return {name => $_}; } @all_names;
then by all means do that.
edit: no, don't do that
The way forward always starts with a minimal test.
| [reply] [d/l] [select] |
|
|
Re: Extracting elements from array
by 1nickt (Canon) on Jan 14, 2016 at 21:17 UTC
|
Well, since the first element in the arrayref doesn't contain a key named 'name', you'll have to deal with that somehow. But what you are looking for is map:
$ perl -wE'
my $aref = [ { name => 'Fred' }, { name => 'Barney' }, { no_name => 'W
+ilma' } ];
say for map { $_->{name} } @{ $aref };
'
Fred
Barney
Use of uninitialized value $_ in say at -e line 3.
Fixing the warning:
$ perl -wE'
my $aref = [ { name => 'Fred' }, { name => 'Barney' }, { no_name => 'W
+ilma' } ];
say for map { $_->{name} || 'No name' } @{ $aref };
'
Fred
Barney
No name
The way forward always starts with a minimal test.
| [reply] [d/l] [select] |
Re: Extracting elements from array
by AnomalousMonk (Archbishop) on Jan 15, 2016 at 00:29 UTC
|
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: <%-{-{-{-<
| [reply] [d/l] [select] |
|
|
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.
| [reply] [d/l] [select] |
|
|
[ 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: <%-{-{-{-<
| [reply] [d/l] [select] |
Re: Extracting elements from array
by shilo (Novice) on Jan 15, 2016 at 15:11 UTC
|
As many here have shown, map is a great (and probably the best) way to get what you are looking for... It is a handy tool, but, it can be a little on the arcane side if you are not used to it. Another option is foreach. If map makes your brain hurt (it does mine sometimes), foreach may be your answer. I use it most often for looping over lists (arrays/hashes/refs of both, etc...). And, if you use labels, you get some pretty easy loop control. Here is the example foreach:
my @new_array;
ELEMENT: foreach my $href (@{$aref}) {
push @new_array, $href if (exists $href->{name});
}
Yes, it seems like more coding than map, and it is... But, it tends to be ultra readable and really obvious about what it is doing. And like @1nickt, I sometimes lean to the more obvious coding approach, so the next poor coder to come along in 6 months does not have to spend the weekend trying to reverse engineer my cleverness (btw, that poor coder is usually me).
| [reply] [d/l] |
|
|
my @newArray = grep {exists $_->{name}} @$aref;
seems a lot cleaner and easier to maintain than the version you offered. Is grep really that much more arcane than using if as a statement modifier, a superfluous statement label and redundant {} when dereferencing a simple array reference?
Premature optimization is the root of all job security
| [reply] [d/l] |
|
|
dereferencing a simple array reference
I always dereference with curly braces, AND whitespace! Sure, it's redundant, but it allows my tired old eyes to see what I just wrote.
keys %$href
keys %{ $href }
I have to squint a little to read the former; not so much on this page, but on the terminal, definitely. :-( But to each their own, eh?
The way forward always starts with a minimal test.
| [reply] [d/l] [select] |
|
|
|
|
|