Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Joining elements of an array in a hash

by kcott (Archbishop)
on Mar 26, 2017 at 21:58 UTC ( [id://1186014]=note: print w/replies, xml ) Need Help??


in reply to Joining elements of an array in a hash

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-24 11:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found