find previous & next in array

J

John Griffiths

hi, anyone know how to do this?

basically i've got an array of items

a = ['a','b','c','d']

now given i can find the position of say 'c' using .index('c'), how do i
get the positions of the ones previous & next to it.

a recursive routine would probably do it, but looking for something more
flash

any ideas?
 
B

Boby Selamet Hartono

[Note: parts of this message were removed to make it a legal post.]

2009/5/14 John Griffiths said:
hi, anyone know how to do this?

basically i've got an array of items

a = ['a','b','c','d']

now given i can find the position of say 'c' using .index('c'), how do i
get the positions of the ones previous & next to it.

a recursive routine would probably do it, but looking for something more
flash

any ideas?
a[a.index:)c)-1] or a[a.index:)c)+1]?
 
J

Joshua Collins

[Note: parts of this message were removed to make it a legal post.]

Boby had it almost right!

instead of:

a[a.index:)c)+1]

it is:

a[a.index('c')+1]

:)

2009/5/14 John Griffiths said:
hi, anyone know how to do this?

basically i've got an array of items

a = ['a','b','c','d']

now given i can find the position of say 'c' using .index('c'), how do i
get the positions of the ones previous & next to it.

a recursive routine would probably do it, but looking for something more
flash

any ideas?
a[a.index:)c)-1] or a[a.index:)c)+1]?
 
B

Boby Selamet Hartono

[Note: parts of this message were removed to make it a legal post.]

2009/5/14 Joshua Collins said:
Boby had it almost right!

instead of:

a[a.index:)c)+1]

it is:

a[a.index('c')+1]

:)

2009/5/14 John Griffiths said:
hi, anyone know how to do this?

basically i've got an array of items

a = ['a','b','c','d']

now given i can find the position of say 'c' using .index('c'), how do i
get the positions of the ones previous & next to it.

a recursive routine would probably do it, but looking for something more
flash

any ideas?
a[a.index:)c)-1] or a[a.index:)c)+1]?

It was a copy from my irb. I'm use symbols as objects in my array. I've
never use index method but since I figure out that method returns an Integer
I think it would be something like that (thanks to irb). I'm a newbie and
just joining this ML yesterday :).
 
R

Robert Klemme

2009/5/14 John Griffiths said:
hi, anyone know how to do this?

basically i've got an array of items

a = ['a','b','c','d']

now given i can find the position of say 'c' using .index('c'), how do i
get the positions of the ones previous & next to it.

a recursive routine would probably do it, but looking for something more
flash

any ideas?

Depends on what you need to do. Do you need those indexes or do you
only need access to those elements?

There are a few variants, but the nice ones aren't really safe against
edge cases (first and last element):

irb(main):004:0> a = ['a','b','c','d']
=> ["a", "b", "c", "d"]
irb(main):005:0> i = a.index('c') and a[i-1 .. i+1]
=> ["b", "c", "d"]
irb(main):006:0> a[a.index('c') - 1, 3] rescue nil
=> ["b", "c", "d"]
irb(main):007:0> i = a.index('a') and a[i-1 .. i+1]
=> []
irb(main):008:0> a[a.index('a') - 1, 3] rescue nil
=> ["d"]

The safest is probably

i = a.index('c')

if i
predecessor = i > 0 ? a[i -1] : nil
successor = a[i + 1] # safe!
end

Or as a variant

i = a.index('c') and begin
predecessor = i > 0 ? a[i -1] : nil
successor = a[i + 1] # safe!
end

Kind regards

robert
 
J

John Griffiths

The safest is probably
i = a.index('c')

if i
predecessor = i > 0 ? a[i -1] : nil
successor = a[i + 1] # safe!
end

Or as a variant

i = a.index('c') and begin
predecessor = i > 0 ? a[i -1] : nil
successor = a[i + 1] # safe!
end

Kind regards

robert

thanks Robert, i was trying to build up a little function to find the
prev/next elements in an array, thanks to your help it's sorted;
couldn't find a solution online. appreciate it all.


John.
 
J

Joshua Collins

[Note: parts of this message were removed to make it a legal post.]

Bobby, I am new too, so I might be missing something as well.

I tried your method first, and when I tried to do the symbol in irb it gave
an error. Maybe you have settings I do not?

The safest is probably

i = a.index('c')

if i
predecessor = i > 0 ? a[i -1] : nil
successor = a[i + 1] # safe!
end

Or as a variant

i = a.index('c') and begin
predecessor = i > 0 ? a[i -1] : nil
successor = a[i + 1] # safe!
end

Kind regards

robert

thanks Robert, i was trying to build up a little function to find the
prev/next elements in an array, thanks to your help it's sorted;
couldn't find a solution online. appreciate it all.


John.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top