in reply to Getting the hash in this format

Hello ash_86, and welcome to the Monastery!

You can use the pairwise function from List::MoreUtils:

#! 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 %hash = pairwise { $a => $b } @array1, @array2; dd \%hash;

Output:

21:57 >perl 977_SoPW.pl { blue => "orange", green => "white", red => "black" } 21:57 >

Hope that helps,

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

Replies are listed 'Best First'.
Re^2: Getting the hash in this format
by ash_86 (Initiate) on Aug 23, 2014 at 12:09 UTC
    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?

      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.