find previous & next in array

Discussion in 'Ruby' started by John Griffiths, May 14, 2009.

  1. 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?
    --
    Posted via http://www.ruby-forum.com/.
     
    John Griffiths, May 14, 2009
    #1
    1. Advertising

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

    2009/5/14 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?
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

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

    --
    Tidak ada yang lebih baik dari kembali ke asal
    Nothing can be better than back to the roots
     
    Boby Selamet Hartono, May 14, 2009
    #2
    1. Advertising

  3. [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]

    :)

    On Wed, May 13, 2009 at 11:48 PM, Boby Selamet Hartono
    <>wrote:

    > 2009/5/14 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?
    > > --
    > > Posted via http://www.ruby-forum.com/.
    > >
    > >

    > a[a.index:)c)-1] or a[a.index:)c)+1]?
    >
    > --
    > Tidak ada yang lebih baik dari kembali ke asal
    > Nothing can be better than back to the roots
    >
     
    Joshua Collins, May 14, 2009
    #3
  4. [Note: parts of this message were removed to make it a legal post.]

    2009/5/14 Joshua Collins <>

    > Boby had it almost right!
    >
    > instead of:
    >
    > a[a.index:)c)+1]
    >
    > it is:
    >
    > a[a.index('c')+1]
    >
    > :)
    >
    > On Wed, May 13, 2009 at 11:48 PM, Boby Selamet Hartono
    > <>wrote:
    >
    > > 2009/5/14 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?
    > > > --
    > > > Posted via http://www.ruby-forum.com/.
    > > >
    > > >

    > > a[a.index:)c)-1] or a[a.index:)c)+1]?
    > >
    > > --
    > > Tidak ada yang lebih baik dari kembali ke asal
    > > Nothing can be better than back to the roots
    > >

    >


    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 :).

    --
    Tidak ada yang lebih baik dari kembali ke asal
    Nothing can be better than back to the roots
     
    Boby Selamet Hartono, May 14, 2009
    #4
  5. 2009/5/14 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?


    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

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, May 14, 2009
    #5
  6. > 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.
    --
    Posted via http://www.ruby-forum.com/.
     
    John Griffiths, May 14, 2009
    #6
  7. [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?

    On Thu, May 14, 2009 at 11:38 AM, John Griffiths <>wrote:

    > > 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.
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Joshua Collins, May 14, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Psybar Phreak

    previous & next buttons

    Psybar Phreak, Oct 11, 2003, in forum: Perl
    Replies:
    3
    Views:
    495
  2. Maziar Aflatoun

    DataGrid Allow Paging next and previous

    Maziar Aflatoun, Jan 25, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    3,133
    Hermit Dave
    Jan 25, 2004
  3. Deniz Bahar
    Replies:
    2
    Views:
    516
    Andrey Tarasevich
    Mar 9, 2005
  4. Boby Selamet Hartono

    find previous & next in array

    Boby Selamet Hartono, May 14, 2009, in forum: Ruby
    Replies:
    0
    Views:
    99
    Boby Selamet Hartono
    May 14, 2009
  5. Tad McClellan
    Replies:
    3
    Views:
    183
    Edward Wijaya
    May 13, 2004
Loading...

Share This Page