Unable to access the elements in an array with array[1][0]

Discussion in 'Ruby' started by Arti Singh, Jul 26, 2010.

  1. Arti Singh

    Arti Singh Guest

    I feel quite silly asking this question, but here I am. I have what I
    believe to be a 2 dimensional array, but I can not access the elements
    in the array with the array syntax array[1][0] when I select the first
    element in the array with array.first or array[1], I get
    array[["first","first value"]] but I am not able to access the string
    "first" or the string "first value"

    my array looks like :
    [["first","first value"]]
    [["second","second value"]]
    [["third","third value"]]
    [["third","third value"]]
    [["fifth","fifth value"]]

    How can I get the elements in this array? am I missing something here?
    --
    Posted via http://www.ruby-forum.com/.
     
    Arti Singh, Jul 26, 2010
    #1
    1. Advertising

  2. Arti Singh

    James Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Mon, Jul 26, 2010 at 10:11 AM, Arti Singh <> wrote:

    > I feel quite silly asking this question, but here I am. I have what I
    > believe to be a 2 dimensional array, but I can not access the elements
    > in the array with the array syntax array[1][0] when I select the first
    > element in the array with array.first or array[1], I get
    > array[["first","first value"]] but I am not able to access the string
    > "first" or the string "first value"
    >
    > my array looks like :
    > [["first","first value"]]
    > [["second","second value"]]
    > [["third","third value"]]
    > [["third","third value"]]
    > [["fifth","fifth value"]]
    >
    > How can I get the elements in this array? am I missing something here?
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    > You don't need double brackets to declare an array.


    ["first","first value"]
    ["second","second value"]
    ["third","third value"]
    ["third","third value"]
    ["fifth","fifth value"]

    You declared an invalid multidimensional array. To declare a
    multidimensional array:

    [["first","first value"], ["second","second value"], ["third","third
    value"], ["third","third value"], ["fifth","fifth value"]]


    Your multidimensional array looks like this:
    [["first","first value"]]

    The second set of brackets are closing off the first multidimensional array.
    At that point, you're trying to access array[1][0], but there's only a
    zeroeth element. Remember: the first element in any array is at offset 0, so
    to access the first element of this multidimensional array: array[0][0]

    Make sense?

    James



    If you're declaring a multid
     
    James, Jul 26, 2010
    #2
    1. Advertising

  3. Arti Singh

    Arti Singh Guest

    Re: Unable to access the elements in an array with array[0]

    Thanks, James. I owe you one.
    I had been reading the data from the datasource incorrectly.
    xclworksheet.Range("A1:B20").rows.each{|r|ea_array.push(r.value.inspect)
    instead I should have just done
    ea_array=xclworksheet.Range("A1:B20")['Value']










    James wrote:
    > On Mon, Jul 26, 2010 at 10:11 AM, Arti Singh <>
    > wrote:
    >
    >> [["third","third value"]]
    >> [["third","third value"]]
    >> [["fifth","fifth value"]]
    >>
    >> How can I get the elements in this array? am I missing something here?
    >> --
    >> Posted via http://www.ruby-forum.com/.
    >>
    >> You don't need double brackets to declare an array.

    >
    > ["first","first value"]
    > ["second","second value"]
    > ["third","third value"]
    > ["third","third value"]
    > ["fifth","fifth value"]
    >
    > You declared an invalid multidimensional array. To declare a
    > multidimensional array:
    >
    > [["first","first value"], ["second","second value"], ["third","third
    > value"], ["third","third value"], ["fifth","fifth value"]]
    >
    >
    > Your multidimensional array looks like this:
    > [["first","first value"]]
    >
    > The second set of brackets are closing off the first multidimensional
    > array.
    > At that point, you're trying to access array[1][0], but there's only a
    > zeroeth element. Remember: the first element in any array is at offset
    > 0, so
    > to access the first element of this multidimensional array: array[0][0]
    >
    > Make sense?
    >
    > James
    >
    >
    >
    > If you're declaring a multid


    --
    Posted via http://www.ruby-forum.com/.
     
    Arti Singh, Jul 26, 2010
    #3
    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. Vikram
    Replies:
    6
    Views:
    654
    Vikram
    Apr 7, 2005
  2. Replies:
    0
    Views:
    343
  3. P
    Replies:
    1
    Views:
    1,182
    Joe Kesselman
    Jul 7, 2006
  4. Naveen Gandham
    Replies:
    0
    Views:
    97
    Naveen Gandham
    Dec 8, 2008
  5. Jake Barnes
    Replies:
    6
    Views:
    414
    9.co.uk
    Feb 8, 2006
Loading...

Share This Page