Get rid of last element in an array

if i have

my_list = “2-131-25-5558-247-68”

and I want to get rid of the last element…
I wrote :

my_list.split(’-’) - my_list.split(’-’).last.to_a =>
“22-13-25-58-47”

which runs well… but isn’t too complex… ?

tfyl

joss

On Feb 20, 2007, at 7:20 AM, Josselin wrote:

if i have

my_list = “2-131-25-5558-247-68”

and I want to get rid of the last element…

I would use a regular expression:

my_list = “2-131-25-5558-247-68”
=> “2-131-25-5558-247-68”

my_list.sub(/-\d+\Z/, “”)
=> “2-131-25-5558-247”

Hope that helps.

James Edward G. II

Josselin wrote:

if i have

my_list = “2-131-25-5558-247-68”

and I want to get rid of the last element…
I wrote :

my_list.split(’-’) - my_list.split(’-’).last.to_a =>
“22-13-25-58-47”

which runs well… but isn’t too complex… ?

tfyl

joss

i’m a newb, and i am probably totally wrong, but the only way to learn
is to test what you think …

won’t the .pop method work? or does that only get rid of the last thing
entered?

Josselin schrieb:

which runs well… but isn’t too complex… ?

tfyl

joss

irb(main):001:0> my_list = “2-131-25-5558-247-68”
=> “2-131-25-5558-247-68”
irb(main):002:0> my_list.split(’-’) - my_list.split(’-’).last.to_a
=> [“2”, “131”, “25”, “5558”, “247”]
irb(main):003:0> my_list.split(’-’)[0…-1].join(’-’)
=> “2-131-25-5558-247-68”
irb(main):004:0> my_list.split(’-’)[0…-2].join(’-’)
=> “2-131-25-5558-247”
irb(main):005:0> my_list.split(’-’)[0…-3].join(’-’)
=> “2-131-25-5558”
irb(main):006:0> my_list.sub(/(?:-\d+)$/, ‘’)
=> “2-131-25-5558-247”
irb(main):007:0> my_list.sub(/(?:-\d+){2}$/, ‘’)
=> “2-131-25-5558”
irb(main):008:0> my_list.sub(/(?:-\d+){3}$/, ‘’)
=> “2-131-25”

Some ideas…

Wolfgang Nádasi-Donner

On Tue, 20 Feb 2007 14:20:07 +0100, Josselin [email protected]
wrote:

my_list = “2-131-25-5558-247-68”

D:\UserPrfs\VALLNERD>irb
irb(main):001:0> “2-131-25-5558-247-68”.split(’-’)[0…-1].join(’-’)
=> “2-131-25-5558-247”

Reads for me a little better than the RE version.

David V.

Array#pop will remove the last element from the original stringified
list,
which might or might not be what you want - the methods in the other
posts
only result in a new string with the last element removed.

Also, #pop will return the popped value, not the original array, which
prevents you from making a single-method-chain-oneliner, a popular
Rubyist
sport :stuck_out_tongue_winking_eye:

David V.

Ah, okay. Well at least i was not COMPLETELY off. :wink:

Derek

On Feb 20, 2:20 pm, Josselin [email protected] wrote:

if i have

my_list = “2-131-25-5558-247-68”

and I want to get rid of the last element…

If you want to keep your list as a string:

my_list.sub(/-\d+$/, ‘’)

or if you want your list as an array:

my_array = my_list.split(/-/)
my_array.delete_at(-1)

Cheers,

Nico

On Tue, 20 Feb 2007 14:33:57 +0100, Derek T.
[email protected] wrote:

won’t the .pop method work? or does that only get rid of the last thing
entered?

Array#pop will remove the last element from the original stringified
list,
which might or might not be what you want - the methods in the other
posts
only result in a new string with the last element removed.

Also, #pop will return the popped value, not the original array, which
prevents you from making a single-method-chain-oneliner, a popular
Rubyist
sport :stuck_out_tongue_winking_eye:

David V.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs