# Array1 + array2 newb question

From: Devin Mullins [mailto:twifkak@comcast.net]
Sent: Thursday, October 13, 2005 6:22 AM
To: ruby-talk ML
Subject: Re: array1 + array2 newb question

AHH! Nevermind. I can't read.

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

leads to
[[[1, "a"], [2, "a"], [3, "a"]], [[1, "b"], [2, "b"], [3, "b"]], [[1,
"c"], [2, "c"], [3, "c"]]]

which isn't equal to

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

cheers

Simon

···

-----Original Message-----

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

Hi --

···

On Thu, 13 Oct 2005, Kroeger, Simon (ext) wrote:

-----Original Message-----
From: Devin Mullins [mailto:twifkak@comcast.net]
Sent: Thursday, October 13, 2005 6:22 AM
To: ruby-talk ML
Subject: Re: array1 + array2 newb question

AHH! Nevermind. I can't read.

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

leads to
[[[1, "a"], [2, "a"], [3, "a"]], [[1, "b"], [2, "b"], [3, "b"]], [[1,
"c"], [2, "c"], [3, "c"]]]

which isn't equal to

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

require flattenx # available on RAA
[1,2,3].map {|m| %w{a b c}.map {|n| [m,n] } }.flatten_once

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

David

--
David A. Black
dblack@wobblini.net