multidimensional array insert syntax

D

Dave Castellano

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
 
J

Jesús Gabriel y Galán

Hi,

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

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


# Randomly insert correct answer.
=A0x =3D rand(4)
=A0answer_choices.insert???????? =A0correct_ans_1
=A0answer_choices.insert???????? =A0correct_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] =3D correct_ans_1
answer_choices[x][1] =3D correct_ano_1

(I assume you wanted to insert the correct_anno_1 in position 1 of the arra=
y).

Jesus.
 
D

Dave Castellano

Jesús Gabriel y Galán said:
� ]


# 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
 
R

Robert Klemme

Jesús Gabriel y Galán said:
� ]


# 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
 
J

Jesús Gabriel y Galán

Jes=C3=BAs Gabriel y Gal=C3=A1n said:
=EF=BF=BD ]


# Randomly insert correct answer.
=EF=BF=BDx =3D rand(4)
=EF=BF=BDanswer_choices.insert???????? =EF=BF=BDcorrect_ans_1
=EF=BF=BDanswer_choices.insert???????? =EF=BF=BDcorrect_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] =3D correct_ans_1
answer_choices[x][1] =3D 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 =3D [1,2,3,4]
=3D> [1, 2, 3, 4]
irb(main):002:0> a[2,0] =3D 55
=3D> 55
irb(main):003:0> a
=3D> [1, 2, 55, 3, 4]
irb(main):004:0> a[2,0] =3D *[100,200,300]
=3D> [100, 200, 300]
irb(main):005:0> a
=3D> [1, 2, 100, 200, 300, 55, 3, 4]

and for multidimensional:

irb(main):006:0> a =3D [[1,2,3],[4,5,6]]
=3D> [[1, 2, 3], [4, 5, 6]]
irb(main):007:0> a[0][1,0] =3D 55
=3D> 55
irb(main):008:0> a
=3D> [[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 []=3D method.

Jesus.
 
J

Jesús Gabriel y Galán

Use Array#insert.

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

Jesus.
 
D

Dave Castellano

Jesús Gabriel y Galán said:
Amazing how I missed that, given that I was specifically looking for
it in the rdoc :D
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([x][1],correct_ans_1) does not work..

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

Thanks,

DC
 
J

Jesús Gabriel y Galán

Jes=FAs Gabriel y Gal=E1n said:
Amazing how I missed that, given that I was specifically looking for
it in the rdoc :D
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([x][1],correct_ans_1) does not work..

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

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[x]

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

inner =3D answer_choices[x]
inner.insert(0, correct_ans_1)

Jesus.
 
D

Dave Castellano

Dave said:
Jesús Gabriel y Galán said:
Amazing how I missed that, given that I was specifically looking for
it in the rdoc :D
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([x][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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top