[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

get rid of last element in an array

Josselin

2/20/2007 1:17:00 PM


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

7 Answers

James Gray

2/20/2007 1:30:00 PM

0

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 Gray II

Derek Derek

2/20/2007 1:34:00 PM

0

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?

--
Posted via http://www.ruby-....

WoNáDo

2/20/2007 1:34:00 PM

0

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

David Vallner

2/20/2007 1:38:00 PM

0

On Tue, 20 Feb 2007 14:20:07 +0100, Josselin <josselin@wanadoo.fr> wrote=
:

> 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

David Vallner

2/20/2007 2:31:00 PM

0

On Tue, 20 Feb 2007 14:33:57 +0100, Derek Teixeira
<derek.teixeira@gmail.com> 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 ;P

David Vallner

Derek Derek

2/20/2007 3:22:00 PM

0

> 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


--
Posted via http://www.ruby-....

Nicolas Desprès

2/21/2007 12:04:00 PM

0



On Feb 20, 2:20 pm, Josselin <josse...@wanadoo.fr> 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