[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: loop over array with indices

Berger, Daniel

5/23/2005 9:18:00 PM

> -----Original Message-----
> From: Johannes Ahlmann [mailto:softpro@gmx.net]
> Sent: Monday, May 23, 2005 3:15 PM
> To: ruby-talk ML
> Subject: loop over array with indices
>
>
> hi,
>
> i have the recurring problem that i want to loop over an
> array with each/collect/... but (as an afterthought) need the
> index of the current element.

See Enumerable#each_with_index.

Regards,

Dan


6 Answers

Brian Schröder

5/24/2005 8:42:00 AM

0

On 23/05/05, Berger, Daniel <Daniel.Berger@qwest.com> wrote:
> > -----Original Message-----
> > From: Johannes Ahlmann [mailto:softpro@gmx.net]
> > Sent: Monday, May 23, 2005 3:15 PM
> > To: ruby-talk ML
> > Subject: loop over array with indices
> >
> >
> > hi,
> >
> > i have the recurring problem that i want to loop over an
> > array with each/collect/... but (as an afterthought) need the
> > index of the current element.
>
> See Enumerable#each_with_index.
>
> Regards,
>
> Dan
>
>

And define yourself map_with_index, inject_with_index etc. where
needed. I always miss them.

best regards,

Brian


--
http://ruby.brian-sch...

Stringed instrument chords: http://chordlist.brian-sch...


Logan Capaldo

5/25/2005 4:24:00 PM

0

On 5/24/05, Brian Schröder <ruby.brian@gmail.com> wrote:
> On 23/05/05, Berger, Daniel <Daniel.Berger@qwest.com> wrote:
> > > -----Original Message-----
> > > From: Johannes Ahlmann [mailto:softpro@gmx.net]
> > > Sent: Monday, May 23, 2005 3:15 PM
> > > To: ruby-talk ML
> > > Subject: loop over array with indices
> > >
> > >
> > > hi,
> > >
> > > i have the recurring problem that i want to loop over an
> > > array with each/collect/... but (as an afterthought) need the
> > > index of the current element.
> >
> > See Enumerable#each_with_index.
> >
> > Regards,
> >
> > Dan
> >
> >
>
> And define yourself map_with_index, inject_with_index etc. where
> needed. I always miss them.
>
> best regards,
>
> Brian
>
>
> --
> http://ruby.brian-sch...
>
> Stringed instrument chords: http://chordlist.brian-sch...
>
>

require 'enumerator'

arr = [ "a", "b", "c", "d" ]

arr_iter = Enumerable::Enumerator.new(arr, :each_with_index)

arr_iter.each do |x, i|
puts "arr[#{i}] => #{x}"
end


#=>
arr[0] => a
arr[1] => b
arr[2] => c
arr[3] => d

arr_iter.collect { |x, i| x[0] + i } #=> [97, 99, 101, 103]

etc. Any methods defined in enumerable should work including each,
map, inject, etc.
inject's args may look a little weird depending on how you call it
ie:

arr_iter.inject("q") { |a, b| a << b[0] << b[1].to_s }
#=> "qa0b1c2d3"

arr_iter.inject { |a, b| (a.kind_of?(Array) ? a[0] + a[1].to_s : a) +
b[0] + b[1].to_s }

#=> "a0b1c2d3"

You may want to write your own inject for this.

require 'enumerator'
module Enumerable
def inject_with_index(*args)
i = Enumerable::Enumerator.new(self, :each_with_index)
if args.empty?
i.inject do |a, b|
yield(a[0], a[1], b[0], b[1])
end
else
i.inject(args[0]) do |a, b|
yield(a, b[0], b[1])
end
end
end
end

Then you can call it less weirdly:

["a", "b", "c"].inject_with_index("") { |a, b, i| a << "#{b}@#{i}" }

#=> "a@0b@1c@2"

["a", "b", "c"].inject_with_index do |a, i1, b, i2|
[a + b, i1 + i2]
end

#=> ["abc", 3]


Pit Capitain

5/25/2005 6:24:00 PM

0

Logan Capaldo schrieb:
>
> You may want to write your own inject for this.
>
> def inject_with_index(*args)
> ...
> end
>
> Then you can call it less weirdly:
>
> ["a", "b", "c"].inject_with_index("") { |a, b, i| a << "#{b}@#{i}" }
> #=> "a@0b@1c@2"
>
> ["a", "b", "c"].inject_with_index do |a, i1, b, i2|
> [a + b, i1 + i2]
> end
> #=> ["abc", 3]

Hi Logan,

note you can have almost the same syntax with the normal inject:

arr_iter.inject("") { |a, (b, i)| a << "#{b}@#{i}" }
# => "a@0b@1c@2"

arr_iter.inject { |(a, i1), (b, i2)| [a + b, i1 + i2] }
# => ["abc", 3]

Regards,
Pit


Logan Capaldo

5/25/2005 8:53:00 PM

0

On 5/25/05, Pit Capitain <pit@capitain.de> wrote:
> Logan Capaldo schrieb:
> >
> > You may want to write your own inject for this.
> >
> > def inject_with_index(*args)
> > ...
> > end
> >
> > Then you can call it less weirdly:
> >
> > ["a", "b", "c"].inject_with_index("") { |a, b, i| a << "#{b}@#{i}" }
> > #=> "a@0b@1c@2"
> >
> > ["a", "b", "c"].inject_with_index do |a, i1, b, i2|
> > [a + b, i1 + i2]
> > end
> > #=> ["abc", 3]
>
> Hi Logan,
>
> note you can have almost the same syntax with the normal inject:
>
> arr_iter.inject("") { |a, (b, i)| a << "#{b}@#{i}" }
> # => "a@0b@1c@2"
>
> arr_iter.inject { |(a, i1), (b, i2)| [a + b, i1 + i2] }
> # => ["abc", 3]
>
> Regards,
> Pit
>
>

Aha. That's really cool. I did not know that.


Yoorghis

7/18/2011 3:31:00 AM

0

On Sun, 17 Jul 2011 19:18:29 -0700 (PDT), Richard Steel
<rsteel2525@aol.com> wrote:

>
>> >In America, we don't consider free speech to be a form of misery.
>
>> Not for dumb assholes whol have sucked up faux snooze crap for years,
>> Steeleloon.
>
>Ah, another Lefty explains why fascism is good when THEY do it.

Fascism is a rightwing ideology you idiot.

>> You don't think Germany would have been better off starting in the
>> 20's to have held Nazi Propagandists to some legitimate standards?
>
>So, book burning is GOOD, when done by people you like?

No, the issue is whacking righgtwingers who lie and cause harm to
society like Hitler and Stalin

>The problem is that your buddies, the Nazi's,
You just said I was a "leftie", you moron

Make up your mind.

IDIOT.




>==================================================================================


>"The great masses' receptive ability is only very limited, their understanding
>is small, but their forgetfullness is great. As a consequence
>of these facts, all effective propagana has to limit itself only to a
>very few points and use them like slogans until even the very last man
>is able to imagine what is intended by such a word.

> Adolph Hitler
> Mein Kampf

Bible Studies with Satan

7/20/2011 4:54:00 PM

0

Yoorghis@Jurgis.net wrote:

> On Wed, 20 Jul 2011 00:31:50 -0700 (PDT), Richard Steel
> <rsteel2525@aol.com> wrote:
>
>>> No, I'm the real American that sees fascist winger excesses as a
>>> threat to the continuity and goodness of America.
>>
>>And you support arresting people who you disagree with.
>
> Once again---When the advocacy of incitement of overthrow of
> government is allued to, or openly advocated by your mad-whackoffs tin
> hatters---It is a GOOD american value to stop them.
>
Fuck off, pussy face.

>
>
>
> COMPARE THIS:
>>================================================
>
> PETE DuPONT, GOPAC Founder: When he began the campaign with GOPAC, the
> serious part of building to a majority, he said, "The first thing
> we've got to do is we all got to use the same language. We've got to
> start talking about"_ and Newt said this hundreds of times_ "start
> talking about a conservative opportunity society replacing the liberal
> Welfare state." And he'd say, "Bang that language into your head. Use
> it every night. Use the same words and pretty soon it will permeate to
> the American people." And that was the right strategy.
>
>
> WITH THIS:
>
>
>
>>==================================================================================
>
>
>>"The great masses' receptive ability is only very limited, their understanding
>>is small, but their forgetfullness is great. As a consequence
>>of these facts, all effective propagana has to limit itself only to a
>>very few points and use them like slogans until even the very last man
>>is able to imagine what is intended by such a word.
>
>> Adolph Hitler
>> Mein Kampf

--
Ezekiel 23:20