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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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'

In reply to Re^5: Processing pairs of values from even-sized array by AnomalousMonk
in thread Processing pairs of values from even-sized array by rovf

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 learning in the Monastery: (7)
As of 2024-04-23 10:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found