mayhem
mayhem

Reputation: 49

how to print an array in particular format in perl?

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

Upvotes: 2

Views: 107

Answers (2)

user557597
user557597

Reputation:

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";
}

Upvotes: 0

mpapec
mpapec

Reputation: 50657

print map "$array1[$_] = $array2[$_]; ", 0 .. $#array1;

Upvotes: 1

Related Questions