Forum: Ruby two arrays problem (although different from the other two ar

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2a0f7bd2c54fbc44329d69555b96f1c5?d=identicon&s=25 Kev Jackson (Guest)
on 2006-03-29 08:49
(Received via mailing list)
I have a bunch of japanese encoded words in Kanji and Hirigana (I
think).  Each set is broken into two pieces, name and type, which can be
combined to produce many more words, so this looks like a classic
'iterate over two arrays problem'

This is what I have, I tried messing with inject and I couldn't get the
desired output.  If anyone wants to golf it I'd like to learn what I
should be doing instead

  corp_data_1.each do |cd1|
      corp_data_2.each do |cd2|
        rd << cd1 + cd2
      end
    end

Thanks
Kev
23172b6630dc631a134c9bad2fec2a39?d=identicon&s=25 ChrisH (Guest)
on 2006-03-29 10:31
(Received via mailing list)
Kev Jackson wrote:
>       corp_data_2.each do |cd2|
>         rd << cd1 + cd2
>       end
>     end
>
> Thanks
> Kev

Not necessarily better but:

rd = corp_data_1.map{|cd1| corp_data_2.map{|cd2| cd1 + cd2}}

cheers
54f9c5392e17646bc29fe6435b55f609?d=identicon&s=25 unknown (Guest)
on 2006-03-29 18:04
(Received via mailing list)
I'm not really clear what you're trying to do. Could you provide a bit
more detail? Do the elements of corp_data1 and corp_data2 correspond to
one another or are they independent? Are you trying to get output with
the same number of elements, or with a number of elements equal to the
product of the two (a sort of cartesian product)?

If it's the former, you should be able to do it with zip and map, if
it's the latter, it seems like what you have should be appropriate.
This topic is locked and can not be replied to.