two arrays problem (although different from the other two arrays)

K

Kev Jackson

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
 
C

ChrisH

Kev said:
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

Not necessarily better but:

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

cheers
 
B

baumanj

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.
 

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

Forum statistics

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

Latest Threads

Top