Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2004
    Location
    UK
    Posts
    9

    Unanswered: Acessing Dynamic Array elements by number

    Hi all...

    I know you can access normal array elements using the myarray[1] type of syntax, but is it possible to access the elements of a dynamic array using the same type of syntax?

    I.e instead of dynArr["name"] use dynArr[2] ?

    Thanks foir the help! Thought I'd accessed them this way before but now it doesn't seem to work!

    Cheers!

  2. #2
    Join Date
    Sep 2003
    Location
    Dallas
    Posts
    182

    A little juggling

    keys in dynamic arrays are always strings instead of integers but you can get around it with a little juggling:

    Say you have a dynamic array "lottastuff" with 12 elements,

    you need to add a fixed string array variable say "lskeys"

    Now you load the elements from lottastuff into lskeys with

    lottastuff.getkeys(lskeys).

    At this point lskeys[1] = the first element in lottastuff
    lskeys[2] = the second element in lottastuff etc.

    Now to address an element in lottastuff directly using integers you simply use

    lottastuff[lskeys[3]] which would be the value of the third element in lottastuff[].

  3. #3
    Join Date
    Aug 2004
    Location
    UK
    Posts
    9
    Thanks very much, didn't think of it that way...

    I got round what I was doing, albeit in a crappy way.. But it wasn't a very nice project I was working on and was glad to finish it heh

    Thanks again!

  4. #4
    Join Date
    Oct 2003
    Posts
    706
    The order in which keys appear in a DynArray (e.g. with forEach) may change at any time. There is no concept of "ordering" of a dynamic array.

    Likewise, in an ordinary Array, there is no concept of keys.
    ChimneySweep(R): fast, automatic
    table repair at a click of the
    mouse! http://www.sundialservices.com

  5. #5
    Join Date
    Sep 2003
    Location
    Dallas
    Posts
    182
    Actually the "order" of the keys don't change. There is no concept of "order." But a virtual order can be established using a standard array as a proxy.

    The solution to the problem is not found in answering the question asked, but in understanding it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •