Other people have mentioned join, which is the right tool for this job. You're also right in thinking of map as being equivalent to the explicit loop. They are actually very similar - in both cases you can actually modify the loop variable (or map variable) and change the underlying array.

Personally, I tend to use map< for "making a new X for every y in the array", i.e. when I want to create a different list of N items, as is the case here. If I want a side-effect (e.g. printing, or calling a function to process every y in the array) I tend to write the explicit loop for clarity. map has that 'pipeline' feel to it.

You mentioned having a leading space on one of the keys, in order to force the sort order.

This is ingenious, but leaves a space in your output and doesn't scale easily to giving your greater control over the sort order. If you want to do that, one way to extend your idea would be to add a prefix, say A_, B_, etc, to each key, in the order you want them displayed.

You can then include the prefix stripping as part of your processing, which brings us to map's close relative (evil twin?) apply. This is almost identical to map, but works on a copy of the items from the array, so it appropriate when you want to change the value of $_ in the processing step (as we do here, to strip off the sorting tag at the front):

#!perl -w use strict; use List::MoreUtils qw/apply/; my %sections=( A_DMA => 'dma', B_FST => 'dma/fst', C_MRA => 'dma/mra', D_BKS => 'bks', E_MSU => 'dma/msu', F_FMA => 'dma/fma', G_FOMC => 'dma/FOMC', ); print join("\n", apply { s/^\w_//; } sort keys %sections);
Note that you need to pull in apply from List::MoreUtils, where you'll also find lots of other goodies in a similar vein, allowing you to replace loops with simpler, hopefully clearer, code.

In reply to Re: Turning a hash into a line of links by jbert
in thread Turning a hash into a line of links by OfficeLinebacker

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.