array1 + array2 newb question

M

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
 
E

ES

Mike said:
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?

E
 
D

Devin Mullins

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

Devin Mullins

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 said:
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] }
 
M

Michael Fellinger

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
 
D

daz

Mike said:
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)
 
M

Mike Schramm

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

Robert Klemme

Mike said:
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
 
Y

Yohanes Santoso

Mike Schramm said:
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
 
W

William James

Mike said:
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}) }
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top