Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: Add new data to array

by BillKSmith (Monsignor)
on Mar 14, 2020 at 18:36 UTC ( [id://11114266]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Add new data to array
in thread Add new data to array

Even better way:
use List::Util qw(first); .. my $add_in = first { $_->{status} eq 'houses' } @$data;
Bill

Replies are listed 'Best First'.
Re^4: Add new data to array
by kcott (Archbishop) on Mar 14, 2020 at 22:00 UTC

    As you're making incremental improvements, here's another. Just a little Sunday morning exercise to get the mental juices flowing. :-)

    Creating the four new_* key-value pairs and doing four // checks, always produces the same result: you only need to do that once. I kept your code up to and including my $add_in = ..., then added these two statements:

    my $add_hashref = { map +('new_' . $_ => $add_in->{$_} // ''), grep $_ ne 'status', keys %$add_in }; %$_ = (%$_, %$add_hashref) for @$data;

    The Dumper output is the same as yours; at least with a fairly thorough visual check, and bearing in mind that key-value pairs are unordered in a hash.

    — Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-25 13:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found