# array1 + array2 newb question

Discussion in 'Ruby' started by Mike Schramm, Oct 13, 2005.

1. ### Mike SchrammGuest

Hey all,

I'm a ruby newbie, so this will be obvious, but I can't figure it out.

I'm trying to create a map grid from arrayed names of streets. I
want to go from [1,2,3] and [a,b,c] to [[1,a], [2,a], [3,a],
[1,b],... [3,c]].

so far the closest I have gotten is

a1.collect!{|x| [x, a2.collect!{|y| y}]}

which returns-- well you can try it if you like, but it's not right.
Instead of just returning one value of a2 at a time, it returns the
whole thing every time. I'm guessing I need to somehow cycle through
each element of a2 (ie a2[0], a2[1], a2[2]), but I can't figure it out.

You guys will probably eat this up. What am I so obviously doing wrong?

Mike

Mike Schramm, Oct 13, 2005

2. ### ESGuest

Mike Schramm wrote:
> Hey all,
>
> I'm a ruby newbie, so this will be obvious, but I can't figure it out.
>
> I'm trying to create a map grid from arrayed names of streets. I want
> to go from [1,2,3] and [a,b,c] to [[1,a], [2,a], [3,a], [1,b],... [3,c]].
>
> so far the closest I have gotten is
>
> a1.collect!{|x| [x, a2.collect!{|y| y}]}
>
> which returns-- well you can try it if you like, but it's not right.
> Instead of just returning one value of a2 at a time, it returns the
> whole thing every time. I'm guessing I need to somehow cycle through
> each element of a2 (ie a2[0], a2[1], a2[2]), but I can't figure it out.
>
> You guys will probably eat this up. What am I so obviously doing wrong?

You are close but there is no cookie for you! You are just constructing
the Array at the wrong place.

[1,2,3].map {|i| [:a, :b, :c].map {|l| [i, l]}}

(Substitute #collect for #map if you wish.) Does that make sense?

> Mike

E

ES, Oct 13, 2005

3. ### Devin MullinsGuest

> I'm trying to create a map grid from arrayed names of streets. I
> want to go from [1,2,3] and [a,b,c] to [[1,a], [2,a], [3,a],
> [1,b],... [3,c]].

Given:

a1=[1,2,3]
a2=%w{a b c}

The brute force way:

a = []; a1.length.times {|i| a << [a1,a2]}

The Ruby Way:

a = a1.zip a2

Plenty of other ways, too, using inject, Enumerator, etc. I'm sure
they'll all be contributed in a few minutes.

collect! is a whole 'nother beast -- it can be munged to do the task at
hand,* but isn't quite appropriate, since it doesn't tell the block what
index you're currently at. Have you read the ri documentation on it?

Devin

*i = -1; a1.collect! { i+=1; [a1,a2] }

Devin Mullins, Oct 13, 2005
4. ### Devin MullinsGuest

a1=[1,2,3]
a2=%w{a b c}
a2.map {|o2| a1.map {|o1| [o1,o2]}}

Devin

Devin Mullins wrote:

>> I'm trying to create a map grid from arrayed names of streets. I
>> want to go from [1,2,3] and [a,b,c] to [[1,a], [2,a], [3,a],
>> [1,b],... [3,c]].

>
>
>
> Given:
>
> a1=[1,2,3]
> a2=%w{a b c}
>
> The brute force way:
>
> a = []; a1.length.times {|i| a << [a1,a2]}
>
> The Ruby Way:
>
> a = a1.zip a2
>
> Plenty of other ways, too, using inject, Enumerator, etc. I'm sure
> they'll all be contributed in a few minutes.
>
> collect! is a whole 'nother beast -- it can be munged to do the task
> at hand,* but isn't quite appropriate, since it doesn't tell the block
> what index you're currently at. Have you read the ri documentation on it?
>
> Devin
>
> *i = -1; a1.collect! { i+=1; [a1,a2] }
>
>
>

Devin Mullins, Oct 13, 2005
5. ### Michael FellingerGuest

hey mike,

my favorite way is
irb(main):001:0> a =3D [1, 2, 3,]
=3D> [1, 2, 3]
irb(main):002:0> b =3D ["a", "b", "c"]
=3D> ["a", "b", "c"]
irb(main):003:0> a.zip(b)
=3D> [[1, "a"], [2, "b"], [3, "c"]]

hope that helps

so long...
manveru

2005/10/13, Mike Schramm <>:
> Hey all,
>
> I'm a ruby newbie, so this will be obvious, but I can't figure it out.
>
> I'm trying to create a map grid from arrayed names of streets. I
> want to go from [1,2,3] and [a,b,c] to [[1,a], [2,a], [3,a],
> [1,b],... [3,c]].
>
> so far the closest I have gotten is
>
> a1.collect!{|x| [x, a2.collect!{|y| y}]}
>
> which returns-- well you can try it if you like, but it's not right.
> Instead of just returning one value of a2 at a time, it returns the
> whole thing every time. I'm guessing I need to somehow cycle through
> each element of a2 (ie a2[0], a2[1], a2[2]), but I can't figure it out.
>
> You guys will probably eat this up. What am I so obviously doing wrong?
>
> Mike
>
>

Michael Fellinger, Oct 13, 2005
6. ### dazGuest

Mike Schramm wrote:
> Hey all,
>
> I'm a ruby newbie, so this will be obvious, but I can't figure it out.
>
> I'm trying to create a map grid from arrayed names of streets. I
> want to go from [1,2,3] and [a,b,c] to [[1,a], [2,a], [3,a],
> [1,b],... [3,c]].
>
> so far the closest I have gotten is
>
> a1.collect!{|x| [x, a2.collect!{|y| y}]}
>

The outer iteration should be on a2 ...

#----------------------------------------------------
ANSWER = [[1, :a], [2, :a], [3, :a], [1, :b], [2, :b], [3, :b], [1, :c], [2, :c], [3, :c]]

a1 = [1, 2, 3]
a2 = [:a, :b, :c]

r = []; a2.each { |e2| r += a1.map {|e1| [e1, e2] } }

#----------------------------------------------------

daz

(Thought I'd better bail out some of the duff answerers ;p)

daz, Oct 13, 2005
7. ### Mike SchrammGuest

Thanks guys, that'll do it.

In the interests of making my little problem applicable to more than
just me, I wasn't correctly thinking about the method as receiving a
block of code, I thought it was going the other way and plugging the
variable in wherever I placed it.

Thanks again for the help.

Mike

>> Hey all,
>> I'm a ruby newbie, so this will be obvious, but I can't figure it
>> out.
>> I'm trying to create a map grid from arrayed names of streets. I
>> want to go from [1,2,3] and [a,b,c] to [[1,a], [2,a], [3,a],
>> [1,b],... [3,c]].
>> so far the closest I have gotten is
>> a1.collect!{|x| [x, a2.collect!{|y| y}]}
>> which returns-- well you can try it if you like, but it's not
>> right. Instead of just returning one value of a2 at a time, it
>> returns the whole thing every time. I'm guessing I need to
>> somehow cycle through each element of a2 (ie a2[0], a2[1], a2
>> [2]), but I can't figure it out.
>> You guys will probably eat this up. What am I so obviously doing
>> wrong?
>>
>>

>
> You are close but there is no cookie for you! You are just
> constructing
> the Array at the wrong place.
>
> [1,2,3].map {|i| [:a, :b, :c].map {|l| [i, l]}}
>
> (Substitute #collect for #map if you wish.) Does that make sense?
>

Mike Schramm, Oct 13, 2005
8. ### Robert KlemmeGuest

Mike Schramm wrote:
> Hey all,
>
> I'm a ruby newbie, so this will be obvious, but I can't figure it out.
>
> I'm trying to create a map grid from arrayed names of streets. I
> want to go from [1,2,3] and [a,b,c] to [[1,a], [2,a], [3,a],
> [1,b],... [3,c]].
>
> so far the closest I have gotten is
>
> a1.collect!{|x| [x, a2.collect!{|y| y}]}
>
> which returns-- well you can try it if you like, but it's not right.
> Instead of just returning one value of a2 at a time, it returns the
> whole thing every time. I'm guessing I need to somehow cycle through
> each element of a2 (ie a2[0], a2[1], a2[2]), but I can't figure it
> out.
>
> You guys will probably eat this up. What am I so obviously doing
> wrong?
>
> Mike

Since there hasn't been an #inject solution so far...

>> a1=[1,2,3]

=> [1, 2, 3]
>> a2=%w{a b c}

=> ["a", "b", "c"]
>> a1.inject([]) {|r,x| a2.each {|y| r << [x,y]}; r}

=> [[1, "a"], [1, "b"], [1, "c"], [2, "a"], [2, "b"], [2, "c"], [3, "a"],
[3, "b"], [3, "c"]]

And the double inject

>> a1.inject([]) {|r,x| a2.inject(r) {|rr,y| rr << [x,y]}}

=> [[1, "a"], [1, "b"], [1, "c"], [2, "a"], [2, "b"], [2, "c"], [3, "a"],
[3, "b"], [3, "c"]]

Note the slight difference to the map approach

>> a1.map {|x| a2.map {|y| [x,y]}}

=> [[[1, "a"], [1, "b"], [1, "c"]], [[2, "a"], [2, "b"], [2, "c"]], [[3,
"a"], [3, "b"], [3, "c"]]]

Here we have one nesting too much (compared to desired results given).

Kind regards

robert

Robert Klemme, Oct 13, 2005
9. ### Yohanes SantosoGuest

Mike Schramm <> writes:

> Hey all,
>
> I'm a ruby newbie, so this will be obvious, but I can't figure it out.
>
> I'm trying to create a map grid from arrayed names of streets. I
> want to go from [1,2,3] and [a,b,c] to [[1,a], [2,a], [3,a],
> [1,b],... [3,c]].

[1,2,3].zip([:a, :b, :c]) #=> [[1, :a], [2, :b], [3, :c]]

YS

Yohanes Santoso, Oct 13, 2005
10. ### William JamesGuest

Mike Schramm wrote:

> I'm trying to create a map grid from arrayed names of streets. I
> want to go from [1,2,3] and [a,b,c] to [[1,a], [2,a], [3,a],
> [1,b],... [3,c]].
>

nums = [1,2,3]
p %w(a b c).inject([]){|a,x| a + nums.zip(Array.new(nums.size){x}) }

William James, Oct 13, 2005