orangepeel1 has asked for the wisdom of the Perl Monks concerning the following question:

I have a hash of arrays
@array1 = ("one", "two", "three", "four", "five"); @array2 = ("banana", "pear", "apple"); my %hash = (numbers => \@array1, fruit => \@array2 );
I would like to go through all the keys to the hash and join the elements of each array. I have tried something like this
foreach my $group (keys %hash) { push @group, @{ $hash {$group}}; foreach (@group) { $statement = join ",", @group; } }
However it does not join them. Does anyone have any suggestions for how I might go about this?

Replies are listed 'Best First'.
Re: Joining elements of an array in a hash
by stevieb (Canon) on Mar 26, 2017 at 17:42 UTC

    You don't need the intermediary @group there, you can do the whole shebang in one go:

    use warnings; use strict; my @array1 = ("one", "two", "three", "four", "five"); my @array2 = ("banana", "pear", "apple"); my %hash = ( numbers => \@array1, fruit => \@array2 ); for my $group (keys %hash){ my $statement = join ', ', @{ $hash{$group} }; print "$statement\n"; }

    Output:

    one, two, three, four, five banana, pear, apple
      great, thank you!
Re: Joining elements of an array in a hash
by kcott (Archbishop) on Mar 26, 2017 at 21:58 UTC

    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

Re: Joining elements of an array in a hash
by tybalt89 (Monsignor) on Mar 26, 2017 at 17:47 UTC

    Like this ?

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1185995 use strict; use warnings; my @array1 = ("one", "two", "three", "four", "five"); my @array2 = ("banana", "pear", "apple"); my %hash = (numbers => \@array1, fruit => \@array2 ); use Data::Dumper; print Dumper \%hash; $_ = join ',', @$_ for values %hash; print Dumper \%hash;
Re: Joining elements of an array in a hash
by 1nickt (Canon) on Mar 26, 2017 at 17:41 UTC

    use strict; use warnings; use feature 'say'; my @array1 = ("one", "two", "three", "four", "five"); my @array2 = ("banana", "pear", "apple"); my %hash = ( numbers => \@array1, fruit => \@array2 ); say "$_: " . join ',', @{ $hash{ $_ } } for keys %hash;
    $ perl 1185995.pl fruit: banana,pear,apple numbers: one,two,three,four,five

    Hope this helps!


    The way forward always starts with a minimal test.
Re: Joining elements of an array in a hash
by Anonymous Monk on Mar 27, 2017 at 17:49 UTC
    This more way efficients:
    printf "numbers: %s\n", join ", ", qw(one two three four five); printf "fruits: %s\n", join ", ", qw(bananar perl apple);