Date: Sat, 08 Sep 2001 17:38:07 GMT
From: Sudhir Krishnan <sudhir@newmail.net>
Subject: Re: sudhir: array references.
Message-Id: <3B9A584C.2D4C825F@newmail.net>

Thanks for the response!
I've tried both setting and retrieving the values in the array as:

a[i][j][k]=value;
and it seems to work.
is this implicitly  the same as a->[i]->[i][k]; ?

I've tried the same with a hash ...
h{"mykey"}[0]=value;
and that works too!

so is there a place where I'm forced to used a->[]?

Also frequently I'm having to get the number of elements in the triple D
array.
I've tried this at different levels:

$#{$a[i][j]} #works
$#{$a[i]}    #works
$#{$a}       #DOES NOT WORK (I usually get -1)

Would u happen to know why the 3rd case does not work?

thanks!
Sudhir


> 
> Sudhir Krishnan wrote:
> 
> > Hi!,
> >
> > I have a triple dimensional array a[i][j][k]
> >
> > It's actually a reference to an reference to an array, thats how
> > it works internally, right?
> >
> > Now if I want to pass it as an argument to a function, how should the
> > prototype be defined and how should the value be returned?
> >
> 
> you pass the reference and return the reference
> 
> example (for 2D, its the same with 3D)
> 
> $matrix=[[1,2],[4,5]];
> 
> now $matrix is a reference to an anonymous array that contains anonymous
> arrays again.
> 
> You can dereference the values like the following
> 
> print $matrix->[0][1];
> or alternatively
> print $matrix->[0]->[1];
> 
> then you subs can look like.
> 
> sub func1
> {
>   my $m=shift;
>   $m->[0][1]=7777;
>   return $m;
> }
> 
> sub main
> {
>   my $matrix=[[1,2],[4,5]];
>   my $matrix2=func1($matrix);
>   print $matrix2->[0][1],"\n";
> }
> 
> If you want to access the values with $matrix[0][1] you need to define the
> structure like this:
> @matrix=([1,2],[4,5]);
> You define an array containing references to arrays and need to pass the
> array (or a pointer to it) as argument.
> imho the above method is much cooler and more logical cause it doesnt mix
> types.
> 
> best,
> peter
> 
> --
> mag. peter pilsl
> pilsl_@goldfisch.at
> http://www.goldfisch.at


