From: Dave Castellano on
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/.

From: Jesús Gabriel y Galán on
On Wed, Jul 28, 2010 at 4:02 AM, Dave Castellano
<dcastellano1(a)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].

You nearly had it in that sentence:

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

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

Jesus.

From: Dave Castellano on
Jesús Gabriel y Galán wrote:
> On Wed, Jul 28, 2010 at 4:02 AM, Dave Castellano
> <dcastellano1(a)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[x][0] = correct_ans_1
> answer_choices[x][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/.

From: Robert Klemme on
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(a)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[x][0] = correct_ans_1
>> answer_choices[x][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

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

From: Jesús Gabriel y Galán on
On Wed, Jul 28, 2010 at 12:56 PM, Dave Castellano
<dcastellano1(a)wideopenwest.com> wrote:
> Jesús Gabriel y Galán wrote:
>> On Wed, Jul 28, 2010 at 4:02 AM, Dave Castellano
>> <dcastellano1(a)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[x][0] = correct_ans_1
>> answer_choices[x][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??

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.