[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

simple noob hash w/ arrays for values question

snowball

7/5/2006 7:06:00 PM

Sorry for the simple question, but I'm new to programming and have been
banging my head over this.

I've got myself a hash with each value an array of strings. I want to
grep the strings and collect only the strings that match the regexp
into an array. How do I go about this?

4 Answers

katherine

7/5/2006 8:56:00 PM

0

my_result_array = []
my_hash.each_value do |my_array|
my_result_array += my_array.select { |item| item =~ /^[A-Za-z]+$/ }
end

The code above would go through and put all of the strings that only
consisted of letters into one array. Is something like this what you
were looking for?

p-hawk wrote:
> Sorry for the simple question, but I'm new to programming and have been
> banging my head over this.
>
> I've got myself a hash with each value an array of strings. I want to
> grep the strings and collect only the strings that match the regexp
> into an array. How do I go about this?

snowball

7/5/2006 9:38:00 PM

0


katherine wrote:
> my_result_array = []
> my_hash.each_value do |my_array|
> my_result_array += my_array.select { |item| item =~ /^[A-Za-z]+$/ }
> end
>
> The code above would go through and put all of the strings that only
> consisted of letters into one array. Is something like this what you
> were looking for?

Yes, this is exactly what I was looking for. Thank you for your help.

*/me goes off to conquer the world*

Erik Veenstra

7/6/2006 9:07:00 AM

0

> I've got myself a hash with each value an array of strings. I
> want to grep the strings and collect only the strings that
> match the regexp into an array. How do I go about this?

my_array = my_hash.values.flatten.select{|s| s =~ /.../}

gegroet,
Erik V. - http://www.erikve...

D'Iberville

11/18/2010 6:37:00 PM

0

Le 10-11-18 07:33, Edith a ?crit :

> Les Nations Unies ont ?chou? avec leur mission de stabilisation en
> Ha?ti et ont plut?t renforc? la crise structurelle du pays, d?clare ce
> vendredi 15 octobre lors d?une conf?rence de presse, Camille Chalmers,
> directeur ex?cutif de la Plateforme de Plaidoyer pour un D?veloppement
> Alternatif (PAPDA). La veille, un sit-in de protestation a eu lieu
> devant le local du minist?re des affaires ?trang?res ? l?initiative de
> plusieurs organisations sociales. Elles ont exig? le d?part imm?diat
> des casques bleus.

Bonne id?e : cessons les ing?rences et laissons ces brave gens se
d?brouiller entre eux...



--
Saviez-vous que... : http://diberville.blo...
Le blogue-notes de Diberville : http://diberville.livejo...
"Davon geht die Welt nicht unter, sieht man sie manchmal auch grau".