PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Delete every other value in an array
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Delete every other value in an array

Réponse
 
LinkBack Outils de la discussion
Vieux 09/05/2008, 15h16   #1
Tim Conner
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Delete every other value in an array

What is the best way to delete every other value in a ruby array?
e.g.
%w(a b c d e f g h i j k)

becomes => [a c e g i k]

thanks
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 09/05/2008, 15h33   #2
yermej
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Delete every other value in an array

On May 9, 9:16 am, Tim Conner <crofty_ja...@hotmail.com> wrote:
> What is the best way to delete every other value in a ruby array?
> e.g.
> %w(a b c d e f g h i j k)
>
> becomes => [a c e g i k]
>
> thanks
> --
> Posted viahttp://www.ruby-forum.com/.


This will work, but I'm not sure if it's the best way.

a = %w(a b c d e f g h i j k)
1.upto(a.size) {|i| a.delete_at i}
  Réponse avec citation
Vieux 09/05/2008, 15h34   #3
Harry Kakueki
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Delete every other value in an array

On Fri, May 9, 2008 at 11:16 PM, Tim Conner <crofty_james@hotmail.com> wrote:
> What is the best way to delete every other value in a ruby array?
> e.g.
> %w(a b c d e f g h i j k)
>
> becomes => [a c e g i k]
>
> thanks
> --
>


Here is one way.

arr = %w[a b c d e f g h i j k]
p arr.select{|x| arr.index(x) % 2 == 0}

Harry

--
A Look into Japanese Ruby List in English
http://www.kakueki.com/ruby/list.html

  Réponse avec citation
Vieux 09/05/2008, 15h37   #4
Raúl Gutiérrez S.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Delete every other value in an array

On Fri, 2008-05-09 at 23:16 +0900, Tim Conner wrote:
> What is the best way to delete every other value in a ruby array?
> e.g.
> %w(a b c d e f g h i j k)
>
> becomes => [a c e g i k]


v = %w(a b c d e f g h i j k)

cnt = 0
v.each { |i|
v.delete(0) if cnt % 2 != 0
cnt += 1
}


rgs


  Réponse avec citation
Vieux 09/05/2008, 15h39   #5
James Gray
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Delete every other value in an array

On May 9, 2008, at 9:16 AM, Tim Conner wrote:

> What is the best way to delete every other value in a ruby array?
> e.g.
> %w(a b c d e f g h i j k)
>
> becomes => [a c e g i k]


>> ary = ("a".."k").to_a

=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]
>> require "enumerator"

=> true
>> ary.enum_slice(2).map { |pair| pair.first }

=> ["a", "c", "e", "g", "i", "k"]

Hope that s.

James Edward Gray II

  Réponse avec citation
Vieux 09/05/2008, 16h08   #6
ara.t.howard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Delete every other value in an array


On May 9, 2008, at 8:35 AM, yermej wrote:
> a = %w(a b c d e f g h i j k)
> 1.upto(a.size) {|i| a.delete_at i}


it does, but accidentally:


cfp:~ > cat a.rb

a = %w(a b c d e f g h i j k)

1.upto(a.size) do |i|
puts '---'
p :i => i
p :before => a
a.delete_at i
p :after => a
end



cfp:~ > ruby a.rb
---
{:i=>1}
{:before=>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=>["a", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=>2}
{:before=>["a", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=>["a", "c", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=>3}
{:before=>["a", "c", "e", "f", "g", "h", "i", "j", "k"]}
{:after=>["a", "c", "e", "g", "h", "i", "j", "k"]}
---
{:i=>4}
{:before=>["a", "c", "e", "g", "h", "i", "j", "k"]}
{:after=>["a", "c", "e", "g", "i", "j", "k"]}
---
{:i=>5}
{:before=>["a", "c", "e", "g", "i", "j", "k"]}
{:after=>["a", "c", "e", "g", "i", "k"]}
---
{:i=>6}
{:before=>["a", "c", "e", "g", "i", "k"]}
{:after=>["a", "c", "e", "g", "i", "k"]}
---
{:i=>7}
{:before=>["a", "c", "e", "g", "i", "k"]}
{:after=>["a", "c", "e", "g", "i", "k"]}
---
{:i=>8}
{:before=>["a", "c", "e", "g", "i", "k"]}
{:after=>["a", "c", "e", "g", "i", "k"]}
---
{:i=>9}
{:before=>["a", "c", "e", "g", "i", "k"]}
{:after=>["a", "c", "e", "g", "i", "k"]}
---
{:i=>10}
{:before=>["a", "c", "e", "g", "i", "k"]}
{:after=>["a", "c", "e", "g", "i", "k"]}
---
{:i=>11}
{:before=>["a", "c", "e", "g", "i", "k"]}
{:after=>["a", "c", "e", "g", "i", "k"]}



look carefully at what's happening for i >= 6.

the indexes map only by accident since each delete modifies the
mapping in the array (size reduced by one after each delete)

a @ http://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




  Réponse avec citation
Vieux 09/05/2008, 16h11   #7
ara.t.howard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Delete every other value in an array


On May 9, 2008, at 8:37 AM, Ra=FAl Guti=E9rrez S. wrote:
> v =3D %w(a b c d e f g h i j k)
>
> cnt =3D 0
> v.each { |i|
> v.delete(0) if cnt % 2 !=3D 0
> cnt +=3D 1
> }


are you sure? ;-)

cfp:~ > cat a.rb
v =3D %w(a b c d e f g h i j k)

cnt =3D 0
v.each do |i|
puts '---'
p :i =3D> i
p :before =3D> v
v.delete(0) if cnt % 2 !=3D 0
cnt +=3D 1
p :after =3D> v
end


puts '=3D=3D=3D'
p v




cfp:~ > ruby a.rb
---
{:i=3D>"a"}
{:before=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=3D>"b"}
{:before=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=3D>"c"}
{:before=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=3D>"d"}
{:before=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=3D>"e"}
{:before=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=3D>"f"}
{:before=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=3D>"g"}
{:before=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=3D>"h"}
{:before=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=3D>"i"}
{:before=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=3D>"j"}
{:before=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=3D>"k"}
{:before=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=3D>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
=3D=3D=3D
["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]



you cannot simultaneously iterate and delete from and enumerable in =20
ruby.



a @ http://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being =20
better. simply reflect on that.
h.h. the 14th dalai lama




  Réponse avec citation
Vieux 09/05/2008, 16h16   #8
ara.t.howard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Delete every other value in an array


On May 9, 2008, at 8:16 AM, Tim Conner wrote:
> What is the best way to delete every other value in a ruby array?
> e.g.
> %w(a b c d e f g h i j k)
>
> becomes => [a c e g i k]
>
> thanks
> --
> Posted via http://www.ruby-forum.com/.
>




cfp:~ > cat a.rb
# the list
a = %w(a b c d e f g h i j k)

# build your index up all at once
evens = Array.new(a.size / 2){|i| i * 2}
ods = Array.new(a.size / 2){|i| i * 2 + 1}

# apply it
p a.values_at(*evens)
p a.values_at(*ods)

# apply it destructively
a.replace a.values_at(*evens)
p a




cfp:~ > ruby a.rb
["a", "c", "e", "g", "i"]
["b", "d", "f", "h", "j"]
["a", "c", "e", "g", "i"]




a @ http://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h57.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,21332 seconds with 16 queries