arrays of arbitrary dimension?

arrays of arbitrary dimension?

am 22.03.2006 22:55:27 von wpegden

Hmm... maybe I've overlooked this in the documentation, but...
I want to be able to have arrays of arbitrary dimension. So not
dimension 1 or 2 or 3, but dimension k. Thus ideally, I would like to
be able to access elements of the array by giving the indices as a 1
dimensional array.

For example:
@index is a (1 dimensional) array of length $k containing nonnegative
integers.

I want to have an array @Vset of dimension $k, and I want to be able to
access the element corresponding to (for example) @index.

So if $k=4 then @index has length 4 and @Vset has dimension 4.

If @index was (1,2,2,3), I would somehow be able to find out the
element $Vset[1][2][2][3]

(Just to reiterate, $k is not fixed).
Thanks very much,
Wes

Re: arrays of arbitrary dimension?

am 22.03.2006 23:58:44 von Eric Teuber

wpegden@hotpop.com wrote:
> Hmm... maybe I've overlooked this in the documentation, but...
> I want to be able to have arrays of arbitrary dimension. So not
> dimension 1 or 2 or 3, but dimension k. Thus ideally, I would like to
> be able to access elements of the array by giving the indices as a 1
> dimensional array.
>
> For example:
> @index is a (1 dimensional) array of length $k containing nonnegative
> integers.
>
> I want to have an array @Vset of dimension $k, and I want to be able to
> access the element corresponding to (for example) @index.
>
> So if $k=4 then @index has length 4 and @Vset has dimension 4.
>
> If @index was (1,2,2,3), I would somehow be able to find out the
> element $Vset[1][2][2][3]
>
> (Just to reiterate, $k is not fixed).

my $count = $#$index;
my $a_dim = "";
while ( $count ge 0 ) {
$a_dim .= "[" . "$index[$count]" ."]";
$count--;
}
print ${Vset${a_dim}};

not tested but maybe an idea.

Eric

--
replace NOSPAM.com by w e b . d e

Re: arrays of arbitrary dimension?

am 23.03.2006 00:00:54 von Matt Garrish

wrote in message
news:1143064527.531143.205460@j33g2000cwa.googlegroups.com.. .

[snip question multiposted to comp.lang.perl.misc]

When you feel the need to post to more than one group, crosspost!

Matt

Re: arrays of arbitrary dimension?

am 23.03.2006 00:20:03 von Eric Teuber

Eric Teuber wrote:
> my $count = $#$index;
> my $a_dim = "";
> while ( $count ge 0 ) {
> $a_dim .= "[" . "$index[$count]" ."]";
> $count--;
> }
> print ${Vset${a_dim}};
>
> not tested but maybe an idea.
>

Of course the counter should start at zero (the other way around) if
this solution is usable.

Eric

--
replace NOSPAM.com by w e b . d e

Re: arrays of arbitrary dimension?

am 23.03.2006 15:20:38 von Paul Lalli

Eric Teuber wrote:
> my $count = $#$index;
> my $a_dim = "";
> while ( $count ge 0 ) {

Why are you using the string comparison operators on numeric arguments?

> $a_dim .= "[" . "$index[$count]" ."]";

please read
perldoc -q quoting

$a_dim .= "[$index[$count]]";

> $count--;
> }
> print ${Vset${a_dim}};
>
> not tested but maybe an idea.

There's really no reason for not testing something when you have no
idea whether or not it would work. The above doesn't. All you have to
do to test that is start perl at the command line, copy and paste the
above, and press CTRL-D. Really not that hard.

The above is trying to call a subroutine named Vset, and has nothing to
do with any variable named @Vset or $Vset.

(FWIW, the error message is: Can't call method "Vset" without a package
or object reference at - line 8.)

Paul Lalli

Re: arrays of arbitrary dimension?

am 04.04.2006 01:42:32 von runester

wpegden@hotpop.com wrote:
> Hmm... maybe I've overlooked this in the documentation, but...
> I want to be able to have arrays of arbitrary dimension. So not
> dimension 1 or 2 or 3, but dimension k. Thus ideally, I would like to
> be able to access elements of the array by giving the indices as a 1
> dimensional array.
>
> For example:
> @index is a (1 dimensional) array of length $k containing nonnegative
> integers.
>
> I want to have an array @Vset of dimension $k, and I want to be able to
> access the element corresponding to (for example) @index.
>
> So if $k=4 then @index has length 4 and @Vset has dimension 4.
>
> If @index was (1,2,2,3), I would somehow be able to find out the
> element $Vset[1][2][2][3]
>
> (Just to reiterate, $k is not fixed).
> Thanks very much,
> Wes
>

This works, and should do what you need. The function "n_dim_index()"
does not do any bounds checking, etc. but would work for any arbitrary
number of dimensions. Note, I used Data::Dumper just to show the sparse
array which was created. I hope this helps.

==========START_CODE==========
#!/usr/bin/perl
use strict;
use Data::Dumper;
my @V;
$V[1][2][2][2] = 'bananas';
$V[1][2][2][3] = 'cherries';
$V[1][2][2][4] = 'apples';
my @index = (1,2,2,3);

print ">>>", n_dim_index(\@V,@index), "<<<\n";
print Dumper \@V;


sub n_dim_index {
my $arrayRef = shift;
my @index = @_;
my $return_value = $arrayRef;
for my $index_pointer (@index) {
$return_value = $return_value->[$index_pointer];
}
return( $return_value );
}
===========END_CODE===========


--
~ runester
[at rocketmail dot com]