Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8189457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:17:04+00:00 2026-06-07T03:17:04+00:00

I have a specific problem that I just can’t find a solution for. I

  • 0

I have a specific problem that I just can’t find a solution for.
I get data in the following format from a text file

    date1 reason1
    date1 reason1
    date1 reason2
    date1 reason3
    date2 reason4
    date2 reason1
    date2 reason2
    date2 reason2
    date2 reason1
    date2 reason3
    date3 reason4
    date3 reason4
    date3 reason1

I want to build statistics on the data e.g. I want to count all different “reasons” from each date like this

    date1 reason1 -> 2        
    date1 reason2 -> 1
    date1 reason3 -> 1
    date2 reason1 -> 2
    date2 reason4 -> 1
    date2 reason2 -> 2
    date1 reason3 -> 1

…and so on. How would I parse the data and build the required result? I guess hashes would be used but I failed to imagine a way to solve this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:17:06+00:00Added an answer on June 7, 2026 at 3:17 am

    Here’s a very straight forward approach:

    h = Hash.new(0)
    File.foreach("foo.txt") do |line|
      h[line.chomp] += 1  
    end  
    h
    #=> {"date1 reason1"=>2,
     "date1 reason2"=>1,
     "date1 reason3"=>1,
     "date2 reason4"=>1,
     "date2 reason1"=>2,
     "date2 reason2"=>2,
     "date2 reason3"=>1,
     "date3 reason4"=>2,
     "date3 reason1"=>1}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem that I have is somehow very specific. I have to implement a
and pardon my english I have a specific problem with Chrome browser, just in
I have an SSIS exportation package that is used to export data from an
I am currently working on a problem that I have an inelegant solution for.
I am having a problem that I just can't seem to figure out. The
I have the following problem that I need to express. There are persons, workplaces
My specific problem is that I have a set of Apache access logs, and
I have a quite specific problem. I created a webapp and want to have
I have seen some related questions but none focusing on the specific problem I
I have run into a bit of a problem here: I had a problem-specific

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.