[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

newbie to ruby, search an array

Chris Ashton

10/16/2008 10:56:00 AM

hi, im fairly new to ruby and cud do with some help

is there an easy way to search an array to see if it contains a desired
value

thanks

e.g. if @joe contains 'bloggs' return true and array location

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

7 Answers

Hoesel, Daniël van

10/16/2008 10:59:00 AM

0

DQpVc2U6IGFycmF5LmluY2x1ZGU/KHZhbHVlKQ0KDQpodHRwOi8vd3d3LnJ1YnktZG9jLm9yZy9j
b3JlL2NsYXNzZXMvQXJyYXkuaHRtbCNNMDAyMjI2DQoNClJlZ2FyZHMsDQoNCkRhbmllbA0KDQot
LS0tLU9yaWdpbmFsIE1lc3NhZ2UtLS0tLQ0KRnJvbTogY2hyaXN0b3BoZXJhc2h0b25AaG90bWFp
bC5jb20gW21haWx0bzpjaHJpc3RvcGhlcmFzaHRvbkBob3RtYWlsLmNvbV0gDQpTZW50OiBkb25k
ZXJkYWcgMTYgb2t0b2JlciAyMDA4IDEyOjU2DQpUbzogcnVieS10YWxrIE1MDQpTdWJqZWN0OiBu
ZXdiaWUgdG8gcnVieSwgc2VhcmNoIGFuIGFycmF5DQoNCmhpLCBpbSBmYWlybHkgbmV3IHRvIHJ1
YnkgYW5kIGN1ZCBkbyB3aXRoIHNvbWUgaGVscA0KDQppcyB0aGVyZSBhbiBlYXN5IHdheSB0byBz
ZWFyY2ggYW4gYXJyYXkgdG8gc2VlIGlmIGl0IGNvbnRhaW5zIGEgZGVzaXJlZA0KdmFsdWUNCg0K
dGhhbmtzDQoNCmUuZy4gaWYgQGpvZSBjb250YWlucyAnYmxvZ2dzJyByZXR1cm4gdHJ1ZSBhbmQg
YXJyYXkgbG9jYXRpb24NCg0KdGhhbmtzDQotLSANClBvc3RlZCB2aWEgaHR0cDovL3d3dy5ydWJ5
LWZvcnVtLmNvbS8uDQoNCg==

WujcioL

10/16/2008 11:03:00 AM

0

Chris Ashton wrote:
> hi, im fairly new to ruby and cud do with some help
>
> is there an easy way to search an array to see if it contains a desired
> value
>
> thanks
>
> e.g. if @joe contains 'bloggs' return true and array location
>
> thanks

Try this:

tab = ["frogs","mugs","bloggs"]
tab.index "bloggs" #=> returns index or nil if not found
--
Posted via http://www.ruby-....

Chris Ashton

10/16/2008 11:35:00 AM

0

thanks for the help, i really appreciate it, worked a treat

I also need to do this:

array[count + 1]

array[count] returns x but array[count + 1] returns nil when i should be
y

am i totally wrong or is it something else?

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

Sebastian Hungerecker

10/16/2008 2:36:00 PM

0

Chris Ashton wrote:
> array[count] returns x but array[count + 1] returns nil when i should be
> y
>
> am i totally wrong or is it something else?

Works here:
>> array=["x","y"]
=> ["x", "y"]
>> count=0
=> 0
>> array[count]
=> "x"
>> array[count+1]
=> "y"

If array[count+1] does not return y for you that simply means, that y is not
the value following x in your array.

HTH,
Sebastian
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Robert Klemme

10/16/2008 3:00:00 PM

0

On 16.10.2008 13:35, Chris Ashton wrote:
> thanks for the help, i really appreciate it, worked a treat
>
> I also need to do this:
>
> array[count + 1]
>
> array[count] returns x but array[count + 1] returns nil when i should be
> y
>
> am i totally wrong or is it something else?

Depends on count and the size of the Array. Either count is array.size
- 1 in which case array[count + 1] points after the last element. Or
your array contains nil at that position.

irb(main):001:0> a=[1,2,3]
=> [1, 2, 3]
irb(main):002:0> a.size
=> 3
irb(main):003:0> a[a.size]
=> nil
irb(main):004:0> a[0]
=> 1
irb(main):005:0> a[1]
=> 2
irb(main):006:0> a[2]
=> 3
irb(main):007:0> a[3]
=> nil
irb(main):008:0>

Cheers

robert

Chris Ashton

10/16/2008 4:02:00 PM

0

thanks for the help, much appreciated, got it going
--
Posted via http://www.ruby-....

Chris Ashton

10/20/2008 1:31:00 PM

0

hi ive changed the original array to a nested one and the tab.index no
longer works is there a way to search and and the index still

thanks for any help
--
Posted via http://www.ruby-....