Multidimensional array insert syntax

Hi,

Can anyone help me with the correct syntax to insert into a
multidimensional array...

answer_choices = [
  [incorrect_ans_2, incorrect_anno_2],
  [incorrect_ans_3, incorrect_anno_3],
  [incorrect_ans_4, incorrect_anno_4],
  [incorrect_ans_5, incorrect_anno_5]
   ]

# Randomly insert correct answer.
x = rand(4)
answer_choices.insert??? correct_ans_1
answer_choices.insert??? correct_anno_1

I want to insert correct_ans_1 into position [x,0] and correct_anno_1
into position [x,0].

Thanks,

DC

···

--
Posted via http://www.ruby-forum.com/.

You nearly had it in that sentence:

answer_choices[0] = correct_ans_1
answer_choices[1] = correct_ano_1

(I assume you wanted to insert the correct_anno_1 in position 1 of the array).

Jesus.

···

On Wed, Jul 28, 2010 at 4:02 AM, Dave Castellano <dcastellano1@wideopenwest.com> wrote:

Hi,

Can anyone help me with the correct syntax to insert into a
multidimensional array...

answer_choices = [
[incorrect_ans_2, incorrect_anno_2],
[incorrect_ans_3, incorrect_anno_3],
[incorrect_ans_4, incorrect_anno_4],
[incorrect_ans_5, incorrect_anno_5]
]

# Randomly insert correct answer.
x = rand(4)
answer_choices.insert??? correct_ans_1
answer_choices.insert??? correct_anno_1

I want to insert correct_ans_1 into position [x,0] and correct_anno_1
into position [x,0].

Jesús Gabriel y Galán wrote:

···

On Wed, Jul 28, 2010 at 4:02 AM, Dave Castellano > <dcastellano1@wideopenwest.com> wrote:

� ]

# Randomly insert correct answer.
�x = rand(4)
�answer_choices.insert??? �correct_ans_1
�answer_choices.insert??? �correct_anno_1

I want to insert correct_ans_1 into position [x,0] and correct_anno_1
into position [x,0].

You nearly had it in that sentence:

answer_choices[0] = correct_ans_1
answer_choices[1] = correct_ano_1

(I assume you wanted to insert the correct_anno_1 in position 1 of the
array).

Jesus.

I actually want to insert rather than replace. Is there a way to do
that??

Thanks!

DC
--
Posted via http://www.ruby-forum.com/\.

http://ruby-doc.org/core/classes/Array.src/M002161.html

irb(main):001:0> a = [1,2,3,4]
=> [1, 2, 3, 4]
irb(main):002:0> a[2,0] = 55
=> 55
irb(main):003:0> a
=> [1, 2, 55, 3, 4]
irb(main):004:0> a[2,0] = *[100,200,300]
=> [100, 200, 300]
irb(main):005:0> a
=> [1, 2, 100, 200, 300, 55, 3, 4]

and for multidimensional:

irb(main):006:0> a = [[1,2,3],[4,5,6]]
=> [[1, 2, 3], [4, 5, 6]]
irb(main):007:0> a[0][1,0] = 55
=> 55
irb(main):008:0> a
=> [[1, 55, 2, 3], [4, 5, 6]]

Take into account that in Ruby arrays are not really
"multidimensional". They are just arrays that contain other arrays.
It's not ensures that each array of a "dimension" has the same length,
for example. When you do a[0] in the above, you get an array, on which
you call the = method.

Jesus.

···

On Wed, Jul 28, 2010 at 12:56 PM, Dave Castellano <dcastellano1@wideopenwest.com> wrote:

Jesús Gabriel y Galán wrote:

On Wed, Jul 28, 2010 at 4:02 AM, Dave Castellano >> <dcastellano1@wideopenwest.com> wrote:

� ]

# Randomly insert correct answer.
�x = rand(4)
�answer_choices.insert??? �correct_ans_1
�answer_choices.insert??? �correct_anno_1

I want to insert correct_ans_1 into position [x,0] and correct_anno_1
into position [x,0].

You nearly had it in that sentence:

answer_choices[0] = correct_ans_1
answer_choices[1] = correct_ano_1

(I assume you wanted to insert the correct_anno_1 in position 1 of the
array).

Jesus.

I actually want to insert rather than replace. Is there a way to do
that??

Use Array#insert.

  robert

···

On 28.07.2010 12:56, Dave Castellano wrote:

Jesús Gabriel y Galán wrote:

On Wed, Jul 28, 2010 at 4:02 AM, Dave Castellano >> <dcastellano1@wideopenwest.com> wrote:

� ]

# Randomly insert correct answer.
�x = rand(4)
�answer_choices.insert??? �correct_ans_1
�answer_choices.insert??? �correct_anno_1

I want to insert correct_ans_1 into position [x,0] and correct_anno_1
into position [x,0].

You nearly had it in that sentence:

answer_choices[0] = correct_ans_1
answer_choices[1] = correct_ano_1

(I assume you wanted to insert the correct_anno_1 in position 1 of the
array).

Jesus.

I actually want to insert rather than replace. Is there a way to do
that??

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Amazing how I missed that, given that I was specifically looking for
it in the rdoc :smiley:
I call "sleep deprived", or something

Jesus.

···

On Wed, Jul 28, 2010 at 1:10 PM, Robert Klemme <shortcutter@googlemail.com> wrote:

Use Array#insert.

Jesús Gabriel y Galán wrote:

Use Array#insert.

Amazing how I missed that, given that I was specifically looking for
it in the rdoc :smiley:
I call "sleep deprived", or something

Jesus.

That works for single dimension arrays but I can't figure out the syntax
for multi...

I have been trying to use "answer_choices.insert" but cannot figure out
the syntax
eg answer_choices.insert([1],correct_ans_1) does not work..

Could you give me an example of Array#insert. using multidim. array

Thanks,

DC

···

On Wed, Jul 28, 2010 at 1:10 PM, Robert Klemme > <shortcutter@googlemail.com> wrote:

--
Posted via http://www.ruby-forum.com/\.

In order to better understand it, I'm afraid you will need to stop
thinking about multidimensional arrays, and start thinking of arrays
of arrays. First you need to get the inner array:

answer_choices

then you get back an array. It's on this inner array where you want to
insert, so (split in two steps):

inner = answer_choices
inner.insert(0, correct_ans_1)

Jesus.

···

On Wed, Jul 28, 2010 at 2:26 PM, Dave Castellano <dcastellano1@wideopenwest.com> wrote:

Jesús Gabriel y Galán wrote:

On Wed, Jul 28, 2010 at 1:10 PM, Robert Klemme >> <shortcutter@googlemail.com> wrote:

Use Array#insert.

Amazing how I missed that, given that I was specifically looking for
it in the rdoc :smiley:
I call "sleep deprived", or something

Jesus.

That works for single dimension arrays but I can't figure out the syntax
for multi...

I have been trying to use "answer_choices.insert" but cannot figure out
the syntax
eg answer_choices.insert([1],correct_ans_1) does not work..

Could you give me an example of Array#insert. using multidim. array

Dave Castellano wrote:

Jesús Gabriel y Galán wrote:

Use Array#insert.

Amazing how I missed that, given that I was specifically looking for
it in the rdoc :smiley:
I call "sleep deprived", or something

Jesus.

That works for single dimension arrays but I can't figure out the syntax
for multi...

I have been trying to use "answer_choices.insert" but cannot figure out
the syntax
eg answer_choices.insert([1],correct_ans_1) does not work..

Could you give me an example of Array#insert. using multidim. array

Thanks,

DC

Sorry, missed your reply earlier. That was what I needed. Thank you
for the help!

DC

···

On Wed, Jul 28, 2010 at 1:10 PM, Robert Klemme >> <shortcutter@googlemail.com> wrote:

--
Posted via http://www.ruby-forum.com/\.