in reply to Using an array element as a loop iterator
Hi gurpreetsingh13,
if I understand want you want done, is to use the index of one array for another, printing out the value in the other one.
First of, I will say use a better variable name instead of a and b like you did.
Secondly, like other monks rightly pointed out, though you have an array variable @a but it contains an arrayref check perlref
That not withstanding you can get what you want done simply by dereferencing the arrayref and loop through using the index to print the corresponding values in the second array like thus:
Of course, the answer you get is 2,4 and 6. Atleast that is what you have using the OP as showed by using the C-style for loop, which you supposed worked and indeed it worked since it gave you your desired result,use warnings; use strict; my @array1 = [ 1, 2, 3 ]; my @array2 = ( 2, 4, 6, 7 ); for ( 0 .. $#{ $array1[0] } ) { print $array2[$_], $/; } ## OR print join $/ => @array2[ 0 .. $#{ $array1[0] } ];
|
|---|