get rid of last element in an array

J

Josselin

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
 
J

James Edward Gray II

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:
=> "2-131-25-5558-247"

Hope that helps.

James Edward Gray II
 
D

Derek Teixeira

Josselin said:
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?
 
W

Wolfgang Nádasi-Donner

Josselin said:
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

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
 
D

David Vallner

my_list =3D "2-131-25-5558-247-68"

D:\UserPrfs\VALLNERD>irb
irb(main):001:0> "2-131-25-5558-247-68".split('-')[0...-1].join('-')
=3D> "2-131-25-5558-247"

Reads for me a little better than the RE version.

David Vallner
 
D

David Vallner

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 ;P

David Vallner
 
D

Derek Teixeira

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 ;P

David Vallner

Ah, okay. Well at least i was not COMPLETELY off. ;)

Derek
 
N

Nicolas Desprès

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
 

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,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top