Friday, February 4, 2011

PERL : Multi-Dimensional Array

pico array1.pl
#!/usr/bin/perl -w
@array1 = (["1","2"],["3","4"],["5","6"]); #multidimensional array
print "$array1[0][0]\n"; #prints 1
$array1ref = [["1","2"],["3","4"],["5","6"]]; #multidimensional array reference
print $array1ref->[0][1];# reference is a scalar value
#END
less data # prints the file on the screen
pico array2.pl
#!/usr/bin/perl -w
open(HAN1, "data1") || die "Error opening file :$!" # $! - system IO errors
while (<HAN1>)
{
push @array1, [ split ]; # split by spaces by default using default variable $_; push appends to an array
}
foreach (@array1)
{
print "@$_\n"; # @$_get the values stored in a multidimensional array
}
print "$array1[0][0]\n"; #prints the first value
print "$array1[0][1]\n"; #prints the second value

No comments:

Post a Comment