in reply to Concordance Printing of Two Arrays
You said you are new so let me explain some things that are not immediately obvious -
$/ - think of this as your new line character (line separator character). see perlvar for more details.
[value1,value2] - creates a reference to the list (value1,value2). Note square brackets
$_ - for/foreach loops populate this variable when we don't specify the looping variable explicitly.
update: missed this one-- @$_[0] and @$_[1] - In the AoA case each element is a "reference" to a list. So you have treat them as arrays when you want individual elements of each ref. $_ in the AoA contains a list now. The @ before $_ helps in dereferencing the list for us.
#!/usr/bin/perl -w use strict; my @num = qw (1 2 3 4 5); my @alp = qw (A B C D E); for (0..$#num) { print $num[$_], "-", $alp[$_],$/; } ### OR CREATE AOA my @AoA = (); for (0..$#num) { push @AoA, [$num[$_], $alp[$_]]; } print $/; foreach (@AoA) { print @$_[0], "-", @$_[1],$/; } __END__ 1-A 2-B 3-C 4-D 5-E 1-A 2-B 3-C 4-D 5-E
|
---|