G'day orangepeel1,

It's unclear whether you want to join all the elements into a single string, or join the elements from each key into separate strings. It's also unclear what your ultimate goal is: to use the joined strings in subsequent processing; print them immediately; save them in a file; something else. To get the best answers, it generally helps to provide an overall picture of what you want to achieve (see "XY Problem" for a more detailed discussion of this).

There's two "Special Variables" you could potentially use: '$,' (the output field separator); '$"' (the list separator).

The following script shows the use of these special variables, for immediate printing or assignment to variables, for both the single and multiple string options.

Note the use of the outer anonymous blocks, and the local function, to localise the changes to the special variables; also the inner anonymous blocks to keep the two $statement variables totally separate.

[Aside: You should generally avoid using the same variable name (e.g. $statement) in the same block of code. I've only done it here for illustrative purposes and to retain the variable name you originally used. Whenever you have a genuine reason for using the same name, I'd strongly recommend keeping them separate with anonymous blocks (as I've shown here). In actual fact, without those anonymous blocks, in the code as written, those variables would still be separate; however, at some later point, when a change is made, one could possibly affect the other — with the anonymous blocks, that won't happen.]

#!/usr/bin/env perl -l use strict; use warnings; my %hash = ( numbers => [qw{one two three four five}], fruit => [qw{banana pear apple}], ); { print 'Direct Printing'; print '-' x 40; local $, = ','; print 'All elements combined:'; print map { @{$hash{$_}} } sort keys %hash; print ''; print 'Elements grouped by hash key:'; print @{$hash{$_}} for sort keys %hash; } print ''; { print 'Printing From Variables'; print '-' x 40; local $" = ','; { print 'All elements combined:'; my $statement = "@{[ map { @{$hash{$_}} } sort keys %hash ]}"; print $statement; } print ''; { print 'Elements grouped by hash key:'; for (sort keys %hash) { my $statement = "@{$hash{$_}}"; print $statement; } } }

Output:

Direct Printing ---------------------------------------- All elements combined: banana,pear,apple,one,two,three,four,five Elements grouped by hash key: banana,pear,apple one,two,three,four,five Printing From Variables ---------------------------------------- All elements combined: banana,pear,apple,one,two,three,four,five Elements grouped by hash key: banana,pear,apple one,two,three,four,five

See also: map and sort.

— Ken


In reply to Re: Joining elements of an array in a hash by kcott
in thread Joining elements of an array in a hash by orangepeel1

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.