newbie to ruby, search an array

C

Chris Ashton

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
 
H

Hoesel, Daniël van

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

Mateusz Tybura

Chris said:
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
 
C

Chris Ashton

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
 
S

Sebastian Hungerecker

Chris said:
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
 
R

Robert Klemme

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
 
R

Roger Reeks

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
 

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,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top