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:
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] } ];
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,
BUT did you check
your "@a" variable after that to see it values? Please do and see what you have!
Using
Data::Dumper
If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author
unknown to me
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.