in reply to Re: Getting the hash in this format
in thread Getting the hash in this format

Thanks Athanasius for looking into this. But the problem is that i will pass on %hash to a function which executes a unix commands based on these parameter. So unix command will as such look like ...-colorPair blue => orange, green => white, red => black and it will fail. I want it like -colorPair blue:orange,green:white,red:black Is this possible?

Replies are listed 'Best First'.
Re^3: Getting the hash in this format
by Athanasius (Archbishop) on Aug 23, 2014 at 12:21 UTC

    Sure, but then you might be better off with another array, rather than a hash as the node title requested:

    #! perl use strict; use warnings; my @array1 = qw(red blue green); my @array2 = qw(black orange white); my @params; push @params, shift(@array1) . ':' . shift(@array2) while @array1; print join(',', @params), "\n";

    Output:

    22:16 >perl 977_SoPW.pl red:black,blue:orange,green:white 22:20 >

    Update: The above removes the elements from @array1 and @array2. For a non-destructive solution, we can again use pairwise:

    #! perl use strict; use warnings; use List::MoreUtils 'pairwise'; use Data::Dump; my @array1 = qw(red blue green); my @array2 = qw(black orange white); my @params = pairwise { $a . ':' . $b } @array1, @array2; dd \@array1; dd \@array2; print join(',', @params), "\n";

    Output:

    23:51 >perl 977_SoPW.pl ["red", "blue", "green"] ["black", "orange", "white"] red:black,blue:orange,green:white 23:51 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Great! Thanks for the help.