Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: Processing pairs of values from even-sized array

by AnomalousMonk (Bishop)
on Jun 04, 2011 at 05:14 UTC ( #908105=note: print w/replies, xml ) Need Help??


in reply to Re^2: Processing pairs of values from even-sized array
in thread Processing pairs of values from even-sized array

List::MoreUtils also has  part to do similar things:

>perl -wMstrict -le "use List::MoreUtils qw(part); use Data::Dumper; ;; my $i; my @parts = part { ++$i % 2 } 1 .. 8; ;; print Dumper \@parts; " $VAR1 = [ [ 2, 4, 6, 8 ], [ 1, 3, 5, 7 ] ];

Replies are listed 'Best First'.
Re^4: Processing pairs of values from even-sized array
by johngg (Canon) on Jun 05, 2011 at 23:19 UTC

    Thank you for pointing that out, I rarely come across List::MoreUtils as it is not a core module so I wasn't aware of part. Very useful if you can work with the resulting AoA but it seems a bit awkward if you really do need separate arrays.

    knoppix@Microknoppix:~$ perl -MList::MoreUtils=part -E ' > @parts = part { > ++ $i; > $i % 5 > ? $i % 2 > ? 0 > : 1 > : 2 > } 1 .. 20; > @odds = @{ $parts[ 0 ] }; > @evens = @{ $parts[ 1 ] }; > @fivers = @{ $parts[ 2 ] }; > say qq{@odds}; > say qq{@evens}; > say qq{@fivers};' 1 3 7 9 11 13 17 19 2 4 6 8 12 14 16 18 5 10 15 20 knoppix@Microknoppix:~$
    knoppix@Microknoppix:~$ perl -E ' > push @{ > $_ % 5 > ? $_ % 2 > ? \ @odds > : \ @evens > : \ @fivers > }, $_ for 1 .. 20; > say qq{@odds}; > say qq{@evens}; > say qq{@fivers};' 1 3 7 9 11 13 17 19 2 4 6 8 12 14 16 18 5 10 15 20 knoppix@Microknoppix:~$

    Perhaps I'm missing something. I have tried pushing references to @odds etc. onto @parts before using part without success so I can't seem get away from having to do @odds = @{ $parts[ 0 ] }; etc.

    Cheers,

    JohnGG

      I have tried pushing references to @odds etc. onto @parts before using part without success ...

      AFAIU, the list of anonymous array references  part returns is entirely independent of anything that has gone before; you would, indeed, be on your own in getting them into a set of named arrays.

      For my money, the two examples you give don't seem all that different in terms of effort needed to generate the final, named arrays. But of course, this is entirely a matter of individual programmer style and taste! I must admit I have never used  part except to become familiar with it.

      The only situation in which I can see a problem developing is if you needed to distribute an input list into a vast number of 'parts'. In this case, I think I would still go with the AoA approach. (Of course, in such a case a purely hash-based approach would probably be even better!) Note that in the example below, the definition of the exact index (i.e., $_ + 7) of each 'part' does not matter, nor does the way they are used in the  part code block, so long as they are all unique and properly associated!

      >perl -wMstrict -lE "use List::MoreUtils qw(part); ;; BEGIN { my @parts = qw(EVENS ODDS FIVERS); eval qq{sub $parts[$_] () { $_ + 7 }} for 0 .. $#parts; } ;; my @parts = part { $_ % 5 == 0 ? FIVERS : $_ % 2 == 0 ? EVENS : ODDS } 0 .. 20 ; ;; say qq{'@{$parts[ODDS]}'}; say qq{'@{$parts[FIVERS]}[1 .. 3]'}; say qq{'@{$parts[EVENS]}'}; " '1 3 7 9 11 13 17 19' '5 10 15' '2 4 6 8 12 14 16 18'

      Update: What I had in mind for a 'purely hash-based' (actually, an HoA) approach:

      >perl -wMstrict -lE "BEGIN { eval qq{sub $_ () { $_ }} for qw(ODDS EVENS FIVERS); } ;; my %parts; for my $n (0 .. 20) { push @{ $parts{ $n % 5 == 0 ? FIVERS : $n % 2 == 0 ? EVENS : ODDS } }, $n; } ;; say qq{'@{$parts{ODDS}}'}; say qq{'@{$parts{FIVERS}}[1 .. 3]'}; say qq{'@{$parts{EVENS}}'}; " '1 3 7 9 11 13 17 19' '5 10 15' '2 4 6 8 12 14 16 18'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2020-10-25 20:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (249 votes). Check out past polls.

    Notices?