arrays of arbitrary dimension?

Discussion in 'Perl Misc' started by wpegden@hotpop.com, Mar 22, 2006.

  1. Guest

    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.)
     
    , Mar 22, 2006
    #1
    1. Advertising

  2. writes:
    > 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
     
    Eric Schwartz, Mar 22, 2006
    #2
    1. Advertising

  3. wrote in news:1143064652.686962.213670
    @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
    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Mar 22, 2006
    #3
  4. <> wrote:

    > 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;
    }
    ------------------------


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 23, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    8,788
    Jim Lewis
    Mar 21, 2006
  2. Honestmath
    Replies:
    5
    Views:
    579
    Honestmath
    Dec 13, 2004
  3. Dimitri Ognibene

    arbitrary dimension array for math

    Dimitri Ognibene, Aug 8, 2006, in forum: Java
    Replies:
    7
    Views:
    1,111
    Stefan Ram
    Aug 8, 2006
  4. Luuk
    Replies:
    15
    Views:
    863
    Nobody
    Feb 11, 2010
  5. Gabriel
    Replies:
    9
    Views:
    299
    Robert Kern
    Jun 4, 2011
Loading...

Share This Page