Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: Add new data to array

by AnomalousMonk (Archbishop)
on Mar 14, 2020 at 17:39 UTC ( [id://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 (Monsignor) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11114265]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-26 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found