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