[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Array.find with index

Rob Redmon

4/24/2009 4:08:00 AM

How do I find matching expressions and return the match and the index of
the match? Actually, I'd be happy with just the index.

Basically, I want the Ruby way to:
1) Given scalar s
2) Given array a1 = [] which monotonically increases (e.g.
[10,11,12,15,16,..])
3) Find the two elements in a1 who are nearest neighbors to the scalar
s. I need the indices more than the values.

Clearly, I can find the nearest values with this:
>> [10,20,30,40,50].find_all{|item| item >= 25 }.first
=> 30
>> [10,20,30,40,50].find_all{|item| item <= 25 }.last
=> 20

How do I get the matched indices? That is, without ugly loops.

R
--
Posted via http://www.ruby-....

9 Answers

Loga Ganesan

4/24/2009 4:58:00 AM

0

Rob Redmon wrote:
> How do I find matching expressions and return the match and the index of
> the match? Actually, I'd be happy with just the index.
>
> Basically, I want the Ruby way to:
> 1) Given scalar s
> 2) Given array a1 = [] which monotonically increases (e.g.
> [10,11,12,15,16,..])
> 3) Find the two elements in a1 who are nearest neighbors to the scalar
> s. I need the indices more than the values.
>
> Clearly, I can find the nearest values with this:
>>> [10,20,30,40,50].find_all{|item| item >= 25 }.first
> => 30
>>> [10,20,30,40,50].find_all{|item| item <= 25 }.last
> => 20
>
> How do I get the matched indices? That is, without ugly loops.
>
> R


Hi friend,
I think that in find_all method, we don't have any provision
to
extract indices alone. But there could be enormous way to achieve your
target.
I suggest you the below code:

irb(main):064:0> a=[10, 20, 30, 40, 50, 60]
=> [10, 20, 30, 40, 50, 60]
irb(main):065:0> c=[];a.each_index{|i| a[i]>20?c<<i:nil }
=> [10, 20, 30, 40, 50, 60]

--
Posted via http://www.ruby-....

lasitha

4/24/2009 5:39:00 AM

0

On Fri, Apr 24, 2009 at 9:38 AM, Rob Redmon <rob.webinator@gmail.com> wrote=
:
> [...]
> Find the two elements in a1 who are nearest neighbors to the scalar
> s. =A0I need the indices more than the values.
>
> Clearly, I can find the nearest values with this:
>>> [10,20,30,40,50].find_all{|item| item >=3D 25 }.first
> =3D> 30
>>> [10,20,30,40,50].find_all{|item| item <=3D 25 }.last
> =3D> 20
>
> How do I get the matched indices? =A0That is, without ugly loops.

#index and #rindex in their block forms come to mind:
http://ruby-doc.org/core-1.9/classes/Array.ht...

cheers,
lasitha

Loga Ganesan

4/24/2009 5:53:00 AM

0

lasitha wrote:
> On Fri, Apr 24, 2009 at 9:38 AM, Rob Redmon <rob.webinator@gmail.com>
> wrote:
>> How do I get the matched indices? �That is, without ugly loops.
> #index and #rindex in their block forms come to mind:
> http://ruby-doc.org/core-1.9/classes/Array.ht...
>
> cheers,
> lasitha

In the URL, they specified the following:
a.rindex{|x|x=="b"}

But the rindex method doesn't provide the syntax of passing the blocks.
I had
tried the following and it thrown the error.

irb(main):012:0> a=[10,20,30,40,50]
=> [10, 20, 30, 40, 50]
irb(main):013:0> a.rindex{|x| x>20}
ArgumentError: wrong number of arguments (0 for 1)
from (irb):13:in `rindex'
from (irb):13
from (null):0
--
Posted via http://www.ruby-....

Heesob Park

4/24/2009 5:55:00 AM

0

Hi,

2009/4/24 Rob Redmon <rob.webinator@gmail.com>:
> How do I find matching expressions and return the match and the index of
> the match? =C2=A0Actually, I'd be happy with just the index.
>
> Basically, I want the Ruby way to:
> 1) Given scalar s
> 2) Given array a1 =3D [] which monotonically increases (e.g.
> [10,11,12,15,16,..])
> 3) Find the two elements in a1 who are nearest neighbors to the scalar
> s. =C2=A0I need the indices more than the values.
>
> Clearly, I can find the nearest values with this:
>>> [10,20,30,40,50].find_all{|item| item >=3D 25 }.first
> =3D> 30
>>> [10,20,30,40,50].find_all{|item| item <=3D 25 }.last
> =3D> 20
>
> How do I get the matched indices? =C2=A0That is, without ugly loops.
>

irb(main):001:0> a =3D [10, 20, 30, 40, 50, 60]
=3D> [10, 20, 30, 40, 50, 60]
irb(main):004:0> (0...a.length).select{|x| a[x]>=3D25}.first
=3D> 2
irb(main):005:0> (0...a.length).select{|x| a[x]<=3D25}.last
=3D> 1

In ruby 1.9.x
irb(main):002:0> a.index{|x| x<=3D25}
=3D> 2
irb(main):003:0> a.rindex{|x| x>=3D25}
=3D> 1

Regards,
Park Heesob

lasitha

4/24/2009 6:22:00 AM

0

On Fri, Apr 24, 2009 at 11:22 AM, Loga Ganesan
<loganathan_gpt@yahoo.co.in> wrote:
> lasitha wrote:
>> On Fri, Apr 24, 2009 at 9:38 AM, Rob Redmon <rob.webinator@gmail.com>
>> wrote:
>>> How do I get the matched indices? =EF=BF=BDThat is, without ugly loops.
>> #index and #rindex in their block forms come to mind:
>> http://ruby-doc.org/core-1.9/classes/Array.ht...
>>
>> cheers,
>> lasitha
>
> In the URL, they specified the following:
> =C2=A0 =C2=A0 a.rindex{|x|x=3D=3D"b"}
>
> But the rindex method doesn't provide the syntax of passing the blocks.
> I had
> tried the following and it thrown the error.
>
> irb(main):012:0> a=3D[10,20,30,40,50]
> =3D> [10, 20, 30, 40, 50]
> irb(main):013:0> a.rindex{|x| x>20}
> ArgumentError: wrong number of arguments (0 for 1)
> =C2=A0 =C2=A0 =C2=A0 =C2=A0from (irb):13:in `rindex'
> =C2=A0 =C2=A0 =C2=A0 =C2=A0from (irb):13
> =C2=A0 =C2=A0 =C2=A0 =C2=A0from (null):0

Hmm, the doco was for 1.9, but it works for 1.8.7 too:

$: ruby -ve 'puts [1, 2, 3].rindex {|e| e =3D=3D 2 }'
ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-darwin8]
1

$: ruby -ve 'puts [1, 2, 3].rindex {|e| e =3D=3D 2 }'
ruby 1.9.1p0 (2009-01-30) [i386-darwin8.11.1]
1

I don't have 1.8.6 to test on this box, i'm afraid.

solidarity,
lasitha.

Jeff

10/17/2011 2:04:00 AM

0

On Oct 16, 8:59 pm, Nil <redno...@REMOVETHIScomcast.net> wrote:
> On 16 Oct 2011, "who?" <yourimageunre...@sbcglobal.net> wrote in
> rec.music.beatles:
>
> > After you've played for many years, it's very easy to
> > hear what a chord is, just by the way it rings out.
> > What I don't understand, is how he memorizes
> > all of those lyrics???????????????????
>
> I assume he's reading them off of a chart. Danny?

I don't see him reading anything. Maybe he is?

RichL

10/17/2011 2:12:00 AM

0

"Nil" <rednoise@REMOVETHIScomcast.net> wrote in message
news:Xns9F80DFBB36955nilch1@130.133.4.11...
> On 16 Oct 2011, "who?" <yourimageunreels@sbcglobal.net> wrote in
> rec.music.beatles:
>
>> After you've played for many years, it's very easy to
>> hear what a chord is, just by the way it rings out.
>> What I don't understand, is how he memorizes
>> all of those lyrics???????????????????
>
> I assume he's reading them off of a chart. Danny?

Having experienced Danny making a YouTube video first-hand, I know the
secret :-)

Donna

10/17/2011 2:52:00 AM

0

On Oct 16, 10:11 pm, "RichL" <rpleav...@yahoo.com> wrote:
> "Nil" <redno...@REMOVETHIScomcast.net> wrote in message
>
> news:Xns9F80DFBB36955nilch1@130.133.4.11...
>
> > On 16 Oct 2011, "who?" <yourimageunre...@sbcglobal.net> wrote in
> > rec.music.beatles:
>
> >> After you've played for many years, it's very easy to
> >> hear what a chord is, just by the way it rings out.
> >> What I don't understand, is how he memorizes
> >> all of those lyrics???????????????????
>
> > I assume he's reading them off of a chart. Danny?
>
> Having experienced Danny making a YouTube video first-hand, I know the
> secret :-)

Spill it, Rich! Do it, do it, do it!

RichL

10/17/2011 11:05:00 PM

0

"Donna" <tom.rach@ix.netcom.com> wrote in message
news:a84219b4-96a4-4091-8c3b-38e0c4dd19aa@y35g2000pre.googlegroups.com...
> On Oct 16, 10:11 pm, "RichL" <rpleav...@yahoo.com> wrote:
>> "Nil" <redno...@REMOVETHIScomcast.net> wrote in message
>>
>> news:Xns9F80DFBB36955nilch1@130.133.4.11...
>>
>> > On 16 Oct 2011, "who?" <yourimageunre...@sbcglobal.net> wrote in
>> > rec.music.beatles:
>>
>> >> After you've played for many years, it's very easy to
>> >> hear what a chord is, just by the way it rings out.
>> >> What I don't understand, is how he memorizes
>> >> all of those lyrics???????????????????
>>
>> > I assume he's reading them off of a chart. Danny?
>>
>> Having experienced Danny making a YouTube video first-hand, I know the
>> secret :-)
>
> Spill it, Rich! Do it, do it, do it!

Well, since he didn't chime in to say what it was, and he didn't warn me
against telling, I suppose there's no harm.

At least for the two videos we made while he was here, he didn't write out
the lyrics entirely. We ran through the songs once or twice, then he just
wrote down a few key words in large print on a piece of paper. Sort of to
remind him what comes next. There were only about 5-6 words written down
per song. So mainly he knew the lyrics, but he needed prompts at certain
points so he wouldn't mix verses up etc.

I think I still have the prompt sheet he made up for "Roll Over Beethoven".
I'll hang onto it until he's famous :-)