stepping through arrays simultaneously

T

tsuraan

Suppose I have two arrays, one holding times, and the other holding
events corresponding to the times. I want to step through both arrays
at the same time. Is there an elegant way to do this? For example:

times = [ '1', '2', '3', '4', '5']
events = [ 'eat', 'sleep', 'eat', 'play', 'sleep' ]

times.size.times { |cnt|
puts times[cnt] + ": " + events[cnt]
}

That's not too pretty; at least, I think the times.size.times {block}
is ugly. Is there a way to join the arrays into an array of n-tuples
and step through them one tuple at a time? I'm a newby, and not
entirely sure what ruby builtins would make sense for this. Thanks!
 
J

James Edward Gray II

Suppose I have two arrays, one holding times, and the other holding
events corresponding to the times. I want to step through both arrays
at the same time. Is there an elegant way to do this? For example:

times = [ '1', '2', '3', '4', '5']
events = [ 'eat', 'sleep', 'eat', 'play', 'sleep' ]

times.size.times { |cnt|
puts times[cnt] + ": " + events[cnt]
}

That's not too pretty; at least, I think the times.size.times {block}
is ugly.

For that, I would use Array#each_index().
Is there a way to join the arrays into an array of n-tuples and step
through them one tuple at a time?

Sure is. You want Array#zip().

Hope that helps.

James Edward Gray II
 
T

tsuraan

Suppose I have two arrays, one holding times, and the other holding
events corresponding to the times. I want to step through both
arrays at the same time. Is there an elegant way to do this? For
example:

times = [ '1', '2', '3', '4', '5']
events = [ 'eat', 'sleep', 'eat', 'play', 'sleep' ]

times.size.times { |cnt|
puts times[cnt] + ": " + events[cnt]
}

That's not too pretty; at least, I think the times.size.times {block}
is ugly.

For that, I would use Array#each_index().

Ok, that looks good. I missed it :)
Sure is. You want Array#zip().

I remember seeing that in pickaxe 2, but it's not in the documentation
of Array. It's in the online docs of course, but it's missing from the
book. Strange. Thanks though; I like that function.
Hope that helps.

Very much! Thanks!

--jay
 
J

James Edward Gray II

I remember seeing that in pickaxe 2, but it's not in the documentation
of Array. It's in the online docs of course, but it's missing from
the book. Strange. Thanks though; I like that function.

My bad. If I had listed it correctly, you would have went right to it.

Array overrides it, but really the method is part of Enumerable. It's
under that listing in the Pickaxe (page 459).

Sorry for spreading confusion.

James Edward Gray II
 
T

tsuraan

My bad. If I had listed it correctly, you would have went right to it.

Array overrides it, but really the method is part of Enumerable. It's
under that listing in the Pickaxe (page 459).

Sorry for spreading confusion.

Aha. Thanks!
 
C

Csaba Henk

times = [ '1', '2', '3', '4', '5']
events = [ 'eat', 'sleep', 'eat', 'play', 'sleep' ]

times.size.times { |cnt|
puts times[cnt] + ": " + events[cnt]
}
Sure is. You want Array#zip().

There is also Array#transpose:

[times, events].transpose.each { |t,e|
puts t + ":" + e
}

Or maybe:

[times, events].transpose.each { |te|
puts te.join(":")
}

Csaba
 
P

Patrick Hurley

I like transpose as well, but FYI there is also the generator library:

require 'generator'
a = [1, 2, 3]
b = ['a', 'b', 'c']
c = [:a, :b, :c]

gen = SyncEnumerator.new(a, b, c)
gen.each {|x, y, z| puts "#{x} #{y} #{z}"

Patrick


times = [ '1', '2', '3', '4', '5']
events = [ 'eat', 'sleep', 'eat', 'play', 'sleep' ]

times.size.times { |cnt|
puts times[cnt] + ": " + events[cnt]
}
Sure is. You want Array#zip().

There is also Array#transpose:

[times, events].transpose.each { |t,e|
puts t + ":" + e
}

Or maybe:

[times, events].transpose.each { |te|
puts te.join(":")
}

Csaba
 
T

tsuraan

times = [ '1', '2', '3', '4', '5']
events = [ 'eat', 'sleep', 'eat', 'play', 'sleep' ]

times.size.times { |cnt|
puts times[cnt] + ": " + events[cnt]
}
Sure is. You want Array#zip().

There is also Array#transpose:

[times, events].transpose.each { |t,e|
puts t + ":" + e
}

Or maybe:

[times, events].transpose.each { |te|
puts te.join(":")
}

Starting to remind me of Matlab :) Is transpose a terribly expensive
operation, compared with zip? Maybe rdoc should list big-O expense for
operations or something.

--jay
 

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,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top