[Melbourne-pm] Nested maps returning hash references
Toby Corkindale
toby.corkindale at strategicdata.com.au
Tue Oct 4 22:44:51 PDT 2011
On 05/10/11 16:41, Andrew Pam wrote:
> On 05/10/11 16:34, Toby Corkindale wrote:
>> OK, this is doing my head in.
>>
>> I have two map functions that work perfectly well on their own:
>>
>> map {
>> $f->can($_) ? ($_ => $f->$_) : ()
>> } qw (label name html);
>>
>> map {
>> {
>> label => $f->label,
>> name => $f->name,
>> html => $f->html,
>> }
>> } @fields;
>>
>>
>> However when I combine them, it all goes pear-shaped, and I just get
>> back one huge, flat array, rather than an array of hashes.
>>
>> map {
>> my $f = $_;
>> {
>> map {
>> $f->can($_) ? ($_ => $f->$_) : ()
>> } qw(label name html);
>> }
>> } @fields;
>>
>>
>> Any thoughts?
>
> You'll probably need to "local $_", but it's recommended that you make
> the inner map into a function and call it from the outer map for
> readability.
Cheers Andrew,
you're right, it'll probably be more readable and better code AND work
right if I go down the subroutine method.
ta,
Toby
More information about the Melbourne-pm
mailing list