Remove first and last items from array

Discussion in 'Ruby' started by Mark Dodwell, Apr 8, 2008.

  1. Mark Dodwell

    Mark Dodwell Guest

    Hi,

    I need to remove the first and last items in an array (which may have 1,
    2 or more items so the resulting array after removal may be empty). I
    came up with this:

    a = [1,2,3,4,5]
    2.times { a.pop; a.reverse! }

    It works well, but do you think that there is a slightly less obscure
    way!?

    Cheers,

    ~ Mark
    --
    Posted via http://www.ruby-forum.com/.
     
    Mark Dodwell, Apr 8, 2008
    #1
    1. Advertising

  2. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Hi,

    "pop"ing the first element of an Array is called "shift" ("unshift"
    being the operation of adding one element
    in front of the array).

    a = [1,2,3,4,5]
    a.pop; a.shift;


    Florian Gilcher

    On Apr 8, 2008, at 6:30 PM, Mark Dodwell wrote:
    > Hi,
    >
    > I need to remove the first and last items in an array (which may
    > have 1,
    > 2 or more items so the resulting array after removal may be empty). I
    > came up with this:
    >
    > a = [1,2,3,4,5]
    > 2.times { a.pop; a.reverse! }
    >
    > It works well, but do you think that there is a slightly less obscure
    > way!?
    >
    > Cheers,
    >
    > ~ Mark
    > --
    > Posted via http://www.ruby-forum.com/.
    >


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.8 (Darwin)

    iEYEARECAAYFAkf7n7oACgkQJA/zY0IIRZbJgQCfclWSiv3lsOeSsBdFFMqWWTDE
    aAIAoI0HoqSXrYbAhsMWRrYKp/Ngx/O0
    =S3a0
    -----END PGP SIGNATURE-----
     
    Florian Gilcher, Apr 8, 2008
    #2
    1. Advertising

  3. Mark Dodwell

    dkmd_nielsen Guest

    On Apr 8, 11:30 am, Mark Dodwell <> wrote:
    > Hi,
    >
    > I need to remove the first and last items in an array (which may have 1,
    > 2 or more items so the resulting array after removal may be empty). I
    > came up with this:
    >
    > a = [1,2,3,4,5]
    > 2.times { a.pop; a.reverse! }
    >
    > It works well, but do you think that there is a slightly less obscure
    > way!?
    >
    > Cheers,
    >
    > ~ Mark
    > --
    > Posted viahttp://www.ruby-forum.com/.


    a.shift
    a.pop
    ?
     
    dkmd_nielsen, Apr 8, 2008
    #3
  4. Mark Dodwell

    Xavier Noria Guest

    On Apr 8, 2008, at 18:30 , Mark Dodwell wrote:

    > I need to remove the first and last items in an array (which may
    > have 1,
    > 2 or more items so the resulting array after removal may be empty). I
    > came up with this:
    >
    > a = [1,2,3,4,5]
    > 2.times { a.pop; a.reverse! }


    Hehe.

    I think the natural idiom is a slice:

    a[1..-2]

    It returns an array as long as a.length > 0.

    -- fxn
     
    Xavier Noria, Apr 8, 2008
    #4
  5. Mark Dodwell

    Mark Dodwell Guest

    Thanks, can't believe I didn't think of the 'shift' operation! Oh well,
    my crazy solution is quite funny I think...

    ~ Mark
    --
    Posted via http://www.ruby-forum.com/.
     
    Mark Dodwell, Apr 8, 2008
    #5
  6. Mark Dodwell

    Kyle Schmitt Guest

    In fact there _is_ slice :)
    which takes the same args.
    a.slice(1..-2)
    and even a destructive
    a.slice!(1..-2)
    but for some reason my ruby really didn't like it... so never mind the
    destructive part....
    On Tue, Apr 8, 2008 at 11:41 AM, Xavier Noria <> wrote:
    > On Apr 8, 2008, at 18:30 , Mark Dodwell wrote:
    > Hehe.
    >
    > I think the natural idiom is a slice:
    >
    > a[1..-2]
     
    Kyle Schmitt, Apr 8, 2008
    #6
  7. [Note: parts of this message were removed to make it a legal post.]

    a.shift
    a.pop

    This reads a little better to me.
    You don't want to collect the items at all right?

    /Shawn

    On Tue, Apr 8, 2008 at 9:30 AM, Mark Dodwell <> wrote:

    > Hi,
    >
    > I need to remove the first and last items in an array (which may have 1,
    > 2 or more items so the resulting array after removal may be empty). I
    > came up with this:
    >
    > a = [1,2,3,4,5]
    > 2.times { a.pop; a.reverse! }
    >
    > It works well, but do you think that there is a slightly less obscure
    > way!?
    >
    > Cheers,
    >
    > ~ Mark
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Shawn Anderson, Apr 8, 2008
    #7
  8. On 08.04.2008 18:56, Kyle Schmitt wrote:
    > In fact there _is_ slice :)
    > which takes the same args.
    > a.slice(1..-2)
    > and even a destructive
    > a.slice!(1..-2)
    > but for some reason my ruby really didn't like it... so never mind the
    > destructive part....
    > On Tue, Apr 8, 2008 at 11:41 AM, Xavier Noria <> wrote:
    >> On Apr 8, 2008, at 18:30 , Mark Dodwell wrote:
    >> Hehe.
    >>
    >> I think the natural idiom is a slice:
    >>
    >> a[1..-2]


    Slice works pretty well - unless the array is small:

    irb(main):004:0> a=(1..5).to_a
    => [1, 2, 3, 4, 5]
    irb(main):005:0> a.slice! 1...-1
    => [2, 3, 4]
    irb(main):006:0> a=[]
    => []
    irb(main):007:0> a.slice! 1...-1
    => nil
    irb(main):008:0> a
    => [nil]
    irb(main):009:0> a=[1]
    => [1]
    irb(main):010:0> a.slice! 1...-1
    => []
    irb(main):011:0>
    irb(main):012:0* a
    => [1]

    Kind regards

    robert
     
    Robert Klemme, Apr 8, 2008
    #8
    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. Patrick G.

    Remove items from GetRows array

    Patrick G., Feb 27, 2004, in forum: ASP General
    Replies:
    2
    Views:
    179
    Patrick G.
    Feb 27, 2004
  2. Josselin
    Replies:
    11
    Views:
    214
    Robert Klemme
    Oct 21, 2006
  3. Robert Klemme
    Replies:
    35
    Views:
    321
    Robert Klemme
    Dec 31, 2007
  4. Shawn W_
    Replies:
    5
    Views:
    288
    Aldric Giacomoni
    Sep 16, 2009
  5. Derek Basch

    Remove array items iteratively

    Derek Basch, Feb 24, 2005, in forum: Javascript
    Replies:
    7
    Views:
    105
    Derek Basch
    Feb 26, 2005
Loading...

Share This Page