i use map alot. particulary when morphing data where i expect every input item to produce an output item. alot of code starts like this.

@out = <>; print Dump(@out);

then successive map like stages...

#!/usr/bin/perl -l use YAML; # whitespace separated column number of hostnames (base 0) $col = shift @ARGV || 0; # hostname other junk -- 0 # other junk hostname junk -- 2 # ... @out = map { [ join('.', @{$_->[0]}), $_->[1], $_->[2] ] } map { [ [ reverse split /\./, $_->[0] ], $_->[1], $_->[2] ] } sort { $a->[0] cmp $b->[0] } map { [ join( '.', reverse @{$_->[0]} ), [ @{$_->[0]} ], $_->[1] ] + } map { [ [ split /\./, $_->[0] ], $_->[1] ] } map { chomp; @f = split; $f = splice @f, $col, 1; [ $f , [ @f ] ] +} <>; # for regular list print "-" x 50; for $x ( @out ) { print "$x->[0] -- @{$x->[2]}"; } # for permuted list print "-" x 50; for $x ( @out ) { for $y ( @{$x->[1]} ) { push @{ $d{$y} }, [ $x->[0], $x->[2] ]; } } for $k ( sort keys %d ) { print "$k:"; print "\t$_->[0] -- @{$_->[1]}" for @{ $d{$k} }; } __END__ order and/or permute hostname with info cat <<_END_ | perl foo.pl $column isc.org tra la la la la juniper.net tra la da la da paradise.gen.nz bra la da la da troublemaking.geek.nz bra la la la la hookup.net woot toot boot automagic.org bling blang blum _END_ -------------------------------------------------- hookup.net -- woot toot boot juniper.net -- tra la da la da troublemaking.geek.nz -- bra la la la la paradise.gen.nz -- bra la da la da automagic.org -- bling blang blum isc.org -- tra la la la la -------------------------------------------------- automagic: automagic.org -- bling blang blum geek: troublemaking.geek.nz -- bra la la la la gen: paradise.gen.nz -- bra la da la da hookup: hookup.net -- woot toot boot isc: isc.org -- tra la la la la juniper: juniper.net -- tra la da la da net: hookup.net -- woot toot boot juniper.net -- tra la da la da nz: troublemaking.geek.nz -- bra la la la la paradise.gen.nz -- bra la da la da org: automagic.org -- bling blang blum isc.org -- tra la la la la paradise: paradise.gen.nz -- bra la da la da troublemaking: troublemaking.geek.nz -- bra la la la la

i find that when i try to do things like this without map that it gets uglier quicker.

i just dig map. i'm looking forward to Perl6 because i think i can use ==> to reverse the maps

@out = <> ==> map { chomp; [ split ] } ==> sort { $somthing } ==> map { sprintf "blah: @$_" } ; print Dump(@out);

In reply to Re: When would you choose foreach instead of map? by Anonymous Monk
in thread When would you choose foreach instead of map? by jpfarmer

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.