From: "FORGHANI,BEHDAD (A-Spokane,ex1)" <[EMAIL PROTECTED]>
> I am trying to make a two dimentional array. I have a function that
> returns an array. The following code works:
>
> @x = f();
> push(@y, \@x);
>
> and then I can reference it @{@y[$i]}[$j};
You should write the code above like this:
$y[$i]->[$j]
or even (which is equivalent, but shorter)
$y[$i]->[$j]
Please also distinguish between
$array[$i]
and
@array[$i]
The first is "the $i-th element of @array", while the second is "a list
containing the i-th element of @array". While it'll give you the same
results most of the time, it's not the same thing.
The @array[...] is so-called "array slice", it allows you to extract
several items in an array at once, but it should not be abused.
($first,$third,$fifth) = @array[0,2,4];
> Is this the best way to make two dimentional arrays; I.e., using push
> and reference to arrays?
Depends on what do you need. The other options would be :
$array[ $i*$rowsize + $j ]
# one dimensional array storing items
# [ 00, 01, 02, 03, 04, 10, 11, 12, 13, 14, 20, 21, ... ]
This one is fine for fixed size arrays. You'd go nuts trying to resize
it and you'll also have to remember the rowsize in a variable and
make sure you do not mix up.
$array{ $i , $j }
# actually a hash using a string containing the indices joined by
# value of $; variable (that is ... by a character that will not
# appear in an index value)
This one is fine for "sparsely populated arrays". That is for arrays
that are mostly empty, only contain a single value now and then.
Also you have to keep in mind that this is actually not an array. So
things like $array[-1] (return the last item in the array) are not
easily done with this implementation, you can't ask what is the
size of a row or column, etc.
> Also, I was wondering if I can construct the array without a variable
> x. I tried: push(@y, \f()); push(@y, \@{f()}); The second line does
> something but not what I intended to do.
Try
push @y, [ f() ];
but it would most probably be better if the f() function already
returned an array reference instead of an array.
It's also be quicker.
Jenda
=========== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ==========
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain
I can't find it.
--- me
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]