Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Ok, now that we understand the real end objective, consider just writing a simple for loop as shown below.

Update: As ++AnomalousMonk points out, my "solution" had the wrong sense on the compare! It should be ne instead of == just as he suggested.

I put the my original code into readmore brackets. The new code with one line of code + comments changed is below.

Note this brings up the issue of whether or not a duplication can occur of a record already in the $all array? see below.

use strict; use warnings; use Data::Dump qw (pp); my $accounts = [ { 'date' => '1980-03-05', 'year' => '2019', 'acc' => '44443', }, { 'date' => '1999-06-19', 'year' => '2017', 'acc' => '54321', }, { 'date' => '1999-09-19', 'year' => '2099', 'acc' => '12345', }, ]; my $all = [ { 'date' => '2000-01-00', 'year' => '2003', 'acc' => '4327', }, { 'date' => '1987-01-03', 'year' => '2013', 'acc' => '89997', }, { 'date' => '1980-04-18', 'year' => '2016', 'acc' => '239876A', }, { 'date' => '1999-06-19', 'year' => '2017', 'acc' => '54321', } ]; # add all hashes from $accounts to $all, but # exclude account 44443, if it is there foreach my $href (@$accounts) { push @$all, $href if $href->{acc} ne '44443'; #note string compar +e } pp $all; __END__ [ { acc => 4327, date => "2000-01-00", year => 2003 }, { acc => 89997, date => "1987-01-03", year => 2013 }, { acc => "239876A", date => "1980-04-18", year => 2016 }, { acc => 54321, date => "1999-06-19", year => 2017 }, { acc => 54321, date => "1999-06-19", year => 2017 }, { acc => 12345, date => "1999-09-19", year => 2099 }, ] Note: account OP's test $account data did contain a record that alread +y exists in the $all array. This results in a duplicate record. See acc=> 54321 above. There are of course ways to prevent that duplication. Before suggesting code for that, I'd like to hear from the OP whether this can really happen in "the real world" and if such a solution is even needed.
Update2:
Well, ok. I will show how to get rid of the dupes.
Because this code illustrates another aspect of Perl.

It is perfectly ok for the input and the output of a grep to be the same array. Perl will keep it straight so that it doesn't "write over its own input".

# make the @$all array the union of everything in # either @$all or @$accounts, excluding the acc 44443 record # (if it exists at all) and exclude any possible duplicates # This "zaps" the duplicate 54321 record. # This literally says, "transfer the input to the output if # the input record isn't about account 44443 and we haven't # already transferred that record to the output before. # This assumes that records are uniquely id'ed by the acc # number. That might or might not be true in the actual # application. my %seen; @$all = grep{ $_->{acc} ne '44443' and !$seen{$_->{acc}}++ } @$all,@$a +ccounts;
Of course this can also be formulated using an explicit for loop.
All greps "hide" a for loop. It is there even if it doesn't quite look that way.
use strict; use warnings; use Data::Dump qw (pp); my $accounts = [ { 'date' => '1980-03-05', 'year' => '2019', 'acc' => '44443', }, { 'date' => '1999-06-19', 'year' => '2017', 'acc' => '54321', }, { 'date' => '1999-09-19', 'year' => '2099', 'acc' => '12345', }, ]; my $all = [ { 'date' => '2000-01-00', 'year' => '2003', 'acc' => '4327', }, { 'date' => '1987-01-03', 'year' => '2013', 'acc' => '89997', }, { 'date' => '1980-04-18', 'year' => '2016', 'acc' => '239876A', }, { 'date' => '1999-06-19', 'year' => '2017', 'acc' => '54321', } ]; # add all hashes from $accounts to $all if # the acc == 44443 foreach my $href (@$accounts) { push @$all, $href if $href->{acc} == 44443; } pp $all; __END__ [ { acc => 4327, date => "2000-01-00", year => 2003 }, { acc => 89997, date => "1987-01-03", year => 2013 }, { acc => "239876A", date => "1980-04-18", year => 2016 }, { acc => 54321, date => "1999-06-19", year => 2017 }, { acc => 44443, date => "1980-03-05", year => 2019 }, ]

In reply to Re^3: Adding only searched values to array ref by Marshall
in thread Adding only searched values to array ref by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (1)
As of 2024-04-24 15:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found