Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

In this arrays the $accounts gets printed twice in a subroutine, I only need to allow the value(s) if this one value is not equal to "44443" as an example, I have a code explanation to try to explain my issue:
I am trying to use "grep", but its giving me a "2" instead, the code is not doing the search.
Any better,correct way to do this?
... print Dumper @$accounts; $VAR1 = { 'date' => '1980-03-05', 'year' => '2019', 'acc' => '44443', }; $VAR2 = { 'date' => '1999-06-19', 'year' => '2017', 'acc' => '54321', }; ...

# Only allow the data if its not equal to the account "44443".
... $accounts = grep { !/44443/ } @$accounts; # It should have this in account:<br> print Dumper $accounts; $VAR2 = { 'date' => '1999-06-19', 'year' => '2017', 'acc' => '54321', }; # Want to add the array $accounts to $all once I have the data I need +in $accounts array. ... my $main_account = "12345"; @{ $all->{ $main_account }}; # Add $accounts to $all if array $accounts had the data I need only. @{ $all->{ $main_account }}; = $accounts; print Dumper $all; $VAR1 = { 'date' => '2000-01-00', 'year' => '2003', 'acc' => '4327', }; $VAR2 = { 'date' => '1987-01-03', 'year' => '2013', 'acc' => '89997', }; $VAR3= { 'date' => '1980-04-18', 'year' => '2016', 'acc' => '239876A', }; $VAR4 = { 'date' => '1999-06-19', 'year' => '2017', 'acc' => '54321', };

Thanks for looking!

Replies are listed 'Best First'.
Re: Adding only searched values to array ref
by choroba (Cardinal) on Sep 20, 2021 at 17:45 UTC
    grep in scalar context returns the number of matches, you need to call it in list context to get the actual matching elements back. In your case, just use the dereference on the left hand side of the assignment, too:
    @$accounts = grep { !/44443/ } @$accounts; # ^ # | # Here.

    Update: But that would work if the elements of @$accounts were strings, but they are not. You need to check the value at the acc key:

    @$accounts = grep { $_->{acc} !~ /44443/ } @$accounts;

    To add the elements from @$accounts to @$all, use push:

    push @$all, @$accounts;

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Adding only searched values to array ref
by AnomalousMonk (Archbishop) on Sep 20, 2021 at 18:02 UTC

    Here's a solution to the first part of what I guess your problem may be. (Please see Short, Self-Contained, Correct Example and How do I post a question effectively? and I know what I mean. Why don't you?)

    Win8 Strawberry 5.8.9.5 (32) Mon 09/20/2021 13:56:49 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings use Data::Dumper; 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 $filtered_accounts = [ grep { $_->{'acc'} ne '54321' } @$accounts ] +; print Dumper $filtered_accounts; ^Z $VAR1 = [ { 'date' => '1980-03-05', 'acc' => '44443', 'year' => '2019' }, { 'date' => '1999-09-19', 'acc' => '12345', 'year' => '2099' } ];

    Update: I don't really understand the rest of the OPed problem.


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

      Once the search is done, it would be adding the two arrays together, $filtered_accounts to $all.

        There are a few ways to add (actually append in this case) one array to another, even if they are array references. A couple:

        Win8 Strawberry 5.8.9.5 (32) Mon 09/20/2021 15:47:19 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings use Data::Dumper; my $ar_1 = [ qw(knights who say ni) ]; my $ar_2 = [ qw(foo bar baz quux) ]; # push @$ar_1, @$ar_2; $ar_1 = [ @$ar_1, @$ar_2 ]; print Dumper $ar_1; ^Z $VAR1 = [ 'knights', 'who', 'say', 'ni', 'foo', 'bar', 'baz', 'quux' ];
        Both methods produce the same results. I prefer the method using push: it's more familiar and readable IMHO, possibly a bit faster. In both cases, a shallow copy of the appended array is done.


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

        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.