Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: iterating through array and saving matches to new array

by jazzfan (Novice)
on Apr 15, 2018 at 21:07 UTC ( [id://1212948]=note: print w/replies, xml ) Need Help??


in reply to iterating through array and saving matches to new array

And the nice benefit of grep is that it works with multiple arrays:

my @b = 1..1000; my @c = 900..1000; my @d = 910..1000; my @newb = grep {$_>850}(@b&&@c&&@d);

Replies are listed 'Best First'.
Re^2: iterating through array and saving matches to new array
by choroba (Cardinal) on Apr 15, 2018 at 21:46 UTC
    && imposes scalar boolean context on the first argument. Therefore, the grep on line 4 iterates over @d only provided @b and @c aren't empty. To iterate over several arrays, use comma:
    my @newb = grep $_ > 850, @b, @c, @d;

    It's also a good idea to test the code before posting here ;-)

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re^2: iterating through array and saving matches to new array (updated)
by AnomalousMonk (Archbishop) on Apr 15, 2018 at 21:41 UTC
    ... it works with multiple arrays:
    ...
    my @newb =  grep {$_>850}(@b&&@c&&@d);

    I'm not sure what you mean by "works" in this context. Is the following output what you expect?

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @b = (1, 2, 3, 11, 12, 13); my @c = (4, 5, 6, 14, 15, 16); my @d = (7, 8, 9, 17, 18, 19); my @newb = grep { $_ > 10 } (@b && @c && @d); dd \@newb; " [17, 18, 19]
    Something seems to have happened to the contents of the  @b and  @c arrays. Again, did you intend this? This result comes from the short-circuiting behavior of the  && || and or logical operators; see perlop. (Please see the non-core module Data::Dump for the dd() function. The core Data::Dumper module has dumper functions that you will not need to install a module to access.)

    Update: Changed the array initializations in the example code above to make the point more clearly.


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

Re^2: iterating through array and saving matches to new array
by Marshall (Canon) on Apr 19, 2018 at 19:56 UTC
    To make the the "or" of all 3 arrays with unique values > 850, consider this:
    #!/usr/bin/perl use strict; use warnings; my @b = 1..1000; my @c = 900..1000; my @d = 910..1000; # my @newb = grep {$_>850}(@b&&@c&&@d); ### will not work! my %seen; my @newb = map{ ($_ > 850 and !$seen{$_}++) ? $_: ()} (@b,@c,@d); print "@newb\n";
    I am not sure what the intent here is.

      Wouldn't the code be more readable/maintainable/etc with List::Util::uniq():
          my @newb = uniq grep $_ > 850, @b, @c, @d;
      Results are the same per Test::More::is_deeply(). (In older Perls, see List::MoreUtils::uniq.)


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

        Yes, actually it would be. Although I would still prefer the block form of grep.
        I show how to use map{}

        It actually took me quite a while to learn how to return "nothing" instead of "undef" from a map.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1212948]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-24 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found