simple noob hash w/ arrays for values question

Discussion in 'Ruby' started by p-hawk, Jul 5, 2006.

  1. p-hawk

    p-hawk Guest

    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?
     
    p-hawk, Jul 5, 2006
    #1
    1. Advertising

  2. p-hawk

    katherine Guest

    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?
     
    katherine, Jul 5, 2006
    #2
    1. Advertising

  3. p-hawk

    p-hawk Guest

    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*
     
    p-hawk, Jul 5, 2006
    #3
  4. > 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.erikveen.dds.nl/
     
    Erik Veenstra, Jul 6, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philipp
    Replies:
    21
    Views:
    1,130
    Philipp
    Jan 20, 2009
  2. rp
    Replies:
    1
    Views:
    534
    red floyd
    Nov 10, 2011
  3. Ronald Fischer

    Hash#keys, Hash#values order question

    Ronald Fischer, Aug 23, 2007, in forum: Ruby
    Replies:
    0
    Views:
    156
    Ronald Fischer
    Aug 23, 2007
  4. Älphä Blüë

    Hash of Hash of Arrays Question

    Älphä Blüë, Jul 18, 2009, in forum: Ruby
    Replies:
    5
    Views:
    673
    Älphä Blüë
    Jul 18, 2009
  5. Tore Aursand
    Replies:
    3
    Views:
    557
    Anno Siegel
    Sep 16, 2003
Loading...

Share This Page