Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > array1 + array2 newb question

Reply
Thread Tools

array1 + array2 newb question

 
 
Mike Schramm
Guest
Posts: n/a
 
      10-13-2005
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


 
Reply With Quote
 
 
 
 
ES
Guest
Posts: n/a
 
      10-13-2005
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




 
Reply With Quote
 
 
 
 
Devin Mullins
Guest
Posts: n/a
 
      10-13-2005
> 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]] }



 
Reply With Quote
 
Devin Mullins
Guest
Posts: n/a
 
      10-13-2005
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]] }
>
>
>




 
Reply With Quote
 
Michael Fellinger
Guest
Posts: n/a
 
      10-13-2005
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 <(E-Mail Removed)>:
> 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
>
>



 
Reply With Quote
 
daz
Guest
Posts: n/a
 
      10-13-2005

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)



 
Reply With Quote
 
Mike Schramm
Guest
Posts: n/a
 
      10-13-2005
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?
>




 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-13-2005
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

 
Reply With Quote
 
Yohanes Santoso
Guest
Posts: n/a
 
      10-13-2005
Mike Schramm <(E-Mail Removed)> 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


 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      10-13-2005

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loading array1.js from xx.js file Sunouchi Javascript 3 01-22-2010 10:26 PM
Beginner: read in lines array1 and after keyword array2 kenslaterpa@hotmail.com Perl Misc 7 07-31-2006 07:09 PM
Cisco Newb Question- sorry indo Cisco 7 05-19-2005 04:20 AM
array2 = array1 -- by Reference or Address Only? Hal Vaughan Javascript 8 02-17-2004 11:43 PM
Find what is in array1 and not in array2 The Poor Perl Misc 3 09-27-2003 06:12 AM



Advertisments