array1 + array2 newb question

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

  1. Mike Schramm

    Mike Schramm Guest

    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
    #1
    1. Advertising

  2. Mike Schramm

    ES Guest

    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
    #2
    1. Advertising

  3. > 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
    #3
  4. 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,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
  5. 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
    #5
  6. Mike Schramm

    daz Guest

    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)
     
    daz, Oct 13, 2005
    #6
  7. Mike Schramm

    Mike Schramm Guest

    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
    #7
  8. 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
    #8
  9. 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
    #9
  10. 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
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. :\\\\derian

    newb question

    :\\\\derian, Oct 17, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    357
    Marina
    Oct 17, 2003
  2. Hal Vaughan
    Replies:
    8
    Views:
    121
    Thomas 'PointedEars' Lahn
    Feb 17, 2004
  3. The Poor
    Replies:
    3
    Views:
    158
    Eric J. Roode
    Sep 27, 2003
  4. Replies:
    7
    Views:
    132
    Marek Stepanek
    Jul 31, 2006
  5. Sunouchi

    Loading array1.js from xx.js file

    Sunouchi, Jan 20, 2010, in forum: Javascript
    Replies:
    3
    Views:
    113
    Sunouchi@
    Jan 22, 2010
Loading...

Share This Page