Array1 + array2 newb question

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 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! :slight_smile: 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

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[i],a2[i]]}

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[i],a2[i]] }

hey mike,

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

hope that helps

so long...
manveru

···

2005/10/13, Mike Schramm <mike@mikeschramm.com>:

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 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] } }

p r, ANSWER
p r == ANSWER
#----------------------------------------------------

daz

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

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... :slight_smile:

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

Mike Schramm <mike@mikeschramm.com> 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

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}) }

AHH! Nevermind. I can't read.

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[i],a2[i]]}

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[i],a2[i]] }

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! :slight_smile: 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?