arrays of arbitrary dimension?

W

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

(I posted this in alt.perl before noticing this was the more
appropriate group.)
 
E

Eric Schwartz

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.

What happened when you tried it? I don't mean to be flip, but this
isn't like you're programming a defibrillator, where the slightest
mistake could cost someone their life. A quick program to test this
shouldn't take more than 10-15 lines or so.
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]

This is perfectly fine. You'll probably want to learn more about
references:

perldoc perlreftut
perldoc perlref

And maybe the 'shift' function:

perldoc -f shift

If you have any code you want advice on, feel free to post it, and we
can help you understand/correct it.

-=Eric
 
A

A. Sinan Unur

(e-mail address removed) wrote in @j33g2000cwa.googlegroups.com:
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.

How does that compare to n dimensions?

Sinan
 
T

Tad McClellan

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.


I give them as a "list" rather than array, but you can easily
change that.

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


------------------------
use warnings;
use strict;

my @Vset = (
[ 11 ],
[ # 1
[ 22 ],
[ 33 ],
[ # 1,2
[ 44],
[ 55],
[ # 1,2,2
66,
77,
88,
99 # 1,2,2,3
]
]
]
);

print get_deep_value( \@Vset, 1,2,2,3 ), "\n";

sub get_deep_value {
my $ref = shift;
foreach ( @_ ) {
$ref = $ref->[ $_ ]; # descend one more level
}
return $ref;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top