[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

stepping through arrays simultaneously

tsuraan

3/22/2005 1:51:00 AM

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!



7 Answers

James Gray

3/22/2005 1:58:00 AM

0

On Mar 21, 2005, at 7:51 PM, tsuraan wrote:

> 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



tsuraan

3/22/2005 2:05:00 AM

0


On Mar 21, 2005, at 8:57 PM, James Edward Gray II wrote:

> On Mar 21, 2005, at 7:51 PM, tsuraan wrote:
>
>> 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 :)

>> 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().

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



James Gray

3/22/2005 2:14:00 AM

0

On Mar 21, 2005, at 8:04 PM, tsuraan wrote:

>>> 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().
>
> 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



tsuraan

3/22/2005 2:48:00 AM

0


On Mar 21, 2005, at 9:13 PM, James Edward Gray II wrote:

> On Mar 21, 2005, at 8:04 PM, tsuraan wrote:
>
>>>> 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().
>>
>> 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.

Aha. Thanks!



Csaba Henk

3/22/2005 2:56:00 AM

0

On 2005-03-22, James Edward Gray II <james@grayproductions.net> wrote:
>> 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

Patrick Hurley

3/22/2005 3:16:00 PM

0

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


On Tue, 22 Mar 2005 12:04:59 +0900, Csaba Henk
<csaba@phony_for_avoiding_spam.org> wrote:
> On 2005-03-22, James Edward Gray II <james@grayproductions.net> wrote:
> >> 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
>
>


tsuraan

3/22/2005 5:51:00 PM

0


On Mar 21, 2005, at 10:04 PM, Csaba Henk wrote:

> On 2005-03-22, James Edward Gray II <james@grayproductions.net> wrote:
>>> 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