String comparisions and counting

Discussion in 'Ruby' started by Stuart Clarke, Nov 5, 2008.

  1. I have an array full of strings which represent a date ID. The array
    contains indivduals strings like the following:

    TueAug052008

    I want to iterate through this array (@eventbydate[]) and check each of
    the values of the array. I then want a statement which says if any of
    the date ID's in the array occurs more than 5 times print out some data.

    Pseudo code

    if dataID occurs more than 5times
    print results
    end

    I hope this makes sense.

    I would appreciate any help

    Regards
    --
    Posted via http://www.ruby-forum.com/.
     
    Stuart Clarke, Nov 5, 2008
    #1
    1. Advertising

  2. Stuart Clarke

    Todd Benson Guest

    On Wed, Nov 5, 2008 at 7:35 AM, Stuart Clarke
    <> wrote:
    > I have an array full of strings which represent a date ID. The array
    > contains indivduals strings like the following:
    >
    > TueAug052008
    >
    > I want to iterate through this array (@eventbydate[]) and check each of
    > the values of the array. I then want a statement which says if any of
    > the date ID's in the array occurs more than 5 times print out some data.
    >
    > Pseudo code
    >
    > if dataID occurs more than 5times
    > print results
    > end
    >
    > I hope this makes sense.
    >
    > I would appreciate any help
    >
    > Regards


    [1, 1, 2, 3, 4].count(1)

    Todd
     
    Todd Benson, Nov 5, 2008
    #2
    1. Advertising

  3. On Wed, Nov 5, 2008 at 10:35 PM, Stuart Clarke
    <> wrote:
    > I have an array full of strings which represent a date ID. The array
    > contains indivduals strings like the following:
    >
    > TueAug052008
    >
    > I want to iterate through this array (@eventbydate[]) and check each of
    > the values of the array. I then want a statement which says if any of
    > the date ID's in the array occurs more than 5 times print out some data.
    >
    > Pseudo code
    >
    > if dataID occurs more than 5times
    > print results
    > end
    >
    > I hope this makes sense.
    >
    > I would appreciate any help
    >
    > Regards
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >




    --
    A Look into Japanese Ruby List in English
    http://www.kakueki.com/ruby/list.html
     
    Harry Kakueki, Nov 5, 2008
    #3
  4. Stuart Clarke wrote:
    > I have an array full of strings which represent a date ID. The array
    > contains indivduals strings like the following:
    >
    > TueAug052008
    >
    > I want to iterate through this array (@eventbydate[]) and check each of
    > the values of the array. I then want a statement which says if any of
    > the date ID's in the array occurs more than 5 times print out some data.


    counts = Hash.new(0)
    @eventbydate.each { |e| counts[e] += 1 }
    if counts.find { |c| c >= 5 }
    puts "Print out some data"
    end

    There are other variations:

    ...
    if counts.values.max >= 5
    ...

    More efficient is to stop counting as soon as you reach 5, if you don't
    need the final values:

    counts = Hash.new(0)
    if @eventbydate.find { |e| (counts[e] += 1) >= 5 }
    puts "Print out some data"
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Nov 5, 2008
    #4
  5. On Wed, Nov 5, 2008 at 10:35 PM, Stuart Clarke
    <> wrote:
    > I have an array full of strings which represent a date ID. The array
    > contains indivduals strings like the following:
    >
    > TueAug052008
    >
    > I want to iterate through this array (@eventbydate[]) and check each of
    > the values of the array. I then want a statement which says if any of
    > the date ID's in the array occurs more than 5 times print out some data.
    >
    > Pseudo code
    >
    > if dataID occurs more than 5times
    > print results
    > end
    >
    > I hope this makes sense.
    >
    > I would appreciate any help
    >
    > Regards
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    Is this helpful?

    arr = ["a","b","a","c","a","a","c"]

    h = Hash.new(0)
    arr.each {|x| h[x] += 1}
    h.each {|x,y| p x if y > 3}

    Harry

    --
    A Look into Japanese Ruby List in English
    http://www.kakueki.com/ruby/list.html
     
    Harry Kakueki, Nov 5, 2008
    #5
  6. Brian Candler, Nov 5, 2008
    #6
  7. Stuart Clarke

    Todd Benson Guest

    On Wed, Nov 5, 2008 at 8:18 AM, Brian Candler <> wrote:
    > Stuart Clarke wrote:
    >> I have an array full of strings which represent a date ID. The array
    >> contains indivduals strings like the following:
    >>
    >> TueAug052008
    >>
    >> I want to iterate through this array (@eventbydate[]) and check each of
    >> the values of the array. I then want a statement which says if any of
    >> the date ID's in the array occurs more than 5 times print out some data.

    >
    > counts = Hash.new(0)
    > @eventbydate.each { |e| counts[e] += 1 }
    > if counts.find { |c| c >= 5 }
    > puts "Print out some data"
    > end
    >
    > There are other variations:
    >
    > ...
    > if counts.values.max >= 5
    > ...
    >
    > More efficient is to stop counting as soon as you reach 5, if you don't
    > need the final values:
    >
    > counts = Hash.new(0)
    > if @eventbydate.find { |e| (counts[e] += 1) >= 5 }
    > puts "Print out some data"
    > end
    > --


    Pretty darn good. Why not use a database? I guess it comes down to
    often you want to query the data.

    Todd
     
    Todd Benson, Nov 5, 2008
    #7
  8. Great stuff thanks. I went the last solution as I only want to pick up
    any any high occurances.

    Many thanks

    Brian Candler wrote:
    > Stuart Clarke wrote:
    >> I have an array full of strings which represent a date ID. The array
    >> contains indivduals strings like the following:
    >>
    >> TueAug052008
    >>
    >> I want to iterate through this array (@eventbydate[]) and check each of
    >> the values of the array. I then want a statement which says if any of
    >> the date ID's in the array occurs more than 5 times print out some data.

    >
    > counts = Hash.new(0)
    > @eventbydate.each { |e| counts[e] += 1 }
    > if counts.find { |c| c >= 5 }
    > puts "Print out some data"
    > end
    >
    > There are other variations:
    >
    > ...
    > if counts.values.max >= 5
    > ...
    >
    > More efficient is to stop counting as soon as you reach 5, if you don't
    > need the final values:
    >
    > counts = Hash.new(0)
    > if @eventbydate.find { |e| (counts[e] += 1) >= 5 }
    > puts "Print out some data"
    > end


    --
    Posted via http://www.ruby-forum.com/.
     
    Stuart Clarke, Nov 5, 2008
    #8
    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. td
    Replies:
    0
    Views:
    492
  2. T Ryi

    Pointer comparisions of same type

    T Ryi, Mar 24, 2010, in forum: C Programming
    Replies:
    4
    Views:
    287
    Richard Bos
    Mar 24, 2010
  3. Eric Mahurin

    parser performance comparisions

    Eric Mahurin, Nov 4, 2005, in forum: Ruby
    Replies:
    9
    Views:
    263
    Steven Jenkins
    Nov 6, 2005
  4. Talib Hussain

    String comparisions

    Talib Hussain, Feb 12, 2009, in forum: Ruby
    Replies:
    4
    Views:
    108
    Robert Klemme
    Feb 12, 2009
  5. Sandman
    Replies:
    7
    Views:
    221
    Anno Siegel
    Aug 3, 2004
Loading...

Share This Page