2

Can anyone tell me how to use 2 array together to print in a particular manner?

@array1= "in_1","in_2","in_3";

@array2= "1","0","1";

I want them to be printed in this pattern

in_1 = 1; in_2 = 0; in_3 =1 ;

thanks

mayhem
  • 49
  • 1
  • 5
  • See: http://stackoverflow.com/questions/38345/is-there-an-elegant-zip-to-interleave-two-lists-in-perl-5 - the general task here is "zip", with the output being secondary. – user2864740 Feb 27 '14 at 21:35
  • 1
    Note: `@array1 = "in_1","in_2","in_3";` should be changed to `@array1 = ("in_1","in_2","in_3");` or similar .. otherwise it'll never work. – user2864740 Feb 27 '14 at 21:44

2 Answers2

1
print map "$array1[$_] = $array2[$_]; ", 0 .. $#array1;
mpapec
  • 50,217
  • 8
  • 67
  • 127
0

Could use a hash slice (associative array)

@ary1 = (a,b,c);
@ary2 = (1,2,3);
%hash = ();
@hash{@ary1} = @ary2;

foreach $key ( keys %hash )
{
    print "$key = $hash{$key}\n";
}

Or just a simple loop.

for (0 .. $#ary1)
{
   print "$ary1[$_] = $ary2[$_]\n";
}