Mike Liss wrote:
>
> I am trying to create a multi-dimensional array
>
> $MyArray[ $SizeOfMyArray ];
>
> So I can do this:
>
> print " $MyArray[ 0 ][ 0 ] \n";
>
>
> And what I want to do is add items to the array dynamically
in Perl, multi-dimensional array is usually implemented with array
reference:
#!/usr/bin/perl -w
use strict;
#--
#-- $h is a reference to an unamed array and each of
#-- the elements in $h is also reference to
#-- unamed array reference. this sort of give you
#-- the feeling of having a muli-dimensional array
#--
my $h = [[1..5],[2..6]];
#--
#-- in Perl, there are a lot of notation to access
#-- each individual element in an array reference.
#-- the following are some of the popular notations.
#-- i am not even trying to show you the others.
#-- they are pretty much the same so just pick your
#-- choice.
#--
#-- the following all prints the number 3 which is
#-- the third (2) element in the first (0) array
#-- reference
#--
print <<ARRAY;
$h->[0]->[2]
$h->[0][2]
@{@{$h}[0]}[2]
@$h[0]->[2]
ARRAY
#--
#-- you mention that you want to add elements to
#-- the array, the following 2 push statment does
#-- just that. again, there are tons of other
#-- notation for doing the same thing
#--
#-- the push statment always puts the newly added
#-- element to the end of the array. if you need
#-- to add the element to another index, just refer
#-- back to the above notations. i am sure you know
#-- what to do :-)
#--
push(@{$h->[0]},99);
push(@{$h->[1]},100);
#--
#-- the following prints the whole array reference out
#-- and confirm that 99 and 100 are indeeded added to the
#-- end of the array reference
#--
print "@{$h->[0]}\n";
print "@{@{$h}[1]}\n";
__END__
you also mention that you want to be able to access your multi-dimensiontal
array like:
$my_array[0][0];
if you really perfer that style, try something like:
my @my_array = ([1..5],[2..6]);
print $my_array[0][0],"\n";
to add to it:
push(@{$my_array[0]},99);
or:
$my_array[0][5] = 99;
it's all about reference. read it. i think i write more than i used to today
:-)
david
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]