Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Add new data to array

by AnomalousMonk (Bishop)
on Mar 14, 2020 at 17:39 UTC ( #11114265=note: print w/replies, xml ) Need Help??


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

It's possible to further simplify this (slightly) by removing the intermediate array variable:
    my ($add_in) = grep { $_->{status} eq 'houses' } @$data;
(And I think it should be noted that the  // defined-or operator was added with Perl version 5.10.)


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

Replies are listed 'Best First'.
Re^3: Add new data to array
by BillKSmith (Prior) on Mar 14, 2020 at 18:36 UTC
    Even better way:
    use List::Util qw(first); .. my $add_in = first { $_->{status} eq 'houses' } @$data;
    Bill

      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
Node Status?
node history
Node Type: note [id://11114265]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2020-05-25 06:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (143 votes). Check out past polls.

    Notices?