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

  • SEARCH
  • Home
  • 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 8946109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:25:38+00:00 2026-06-15T12:25:38+00:00

I am parsing a log file and reading line by line in ruby I

  • 0

I am parsing a log file and reading line by line in ruby

I will get 2 values like:

iphone mobile
mobile iphone

kindle amazon
amazon kindle

What I need is count i.e. though both the values are swapped like (a,b) and (b,a) ..but it should be counted as 1 and need result as follows

“iphone mobile” => 2
“kindle amazon” => 2

Thank you!

  • 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-15T12:25:39+00:00Added an answer on June 15, 2026 at 12:25 pm

    Here is something that will work. A sorted array of the input line is used as the key for the hash:

    #!/usr/bin/ruby
    
    # Set the default value of the hash to be zero instead of nil
    h = Hash.new 0
    
    # Process each line of the input file
    ARGF.each do |line|
        # Split the line into a sorted array of words
        values = line.chomp.split.sort
    
        # Skip this line if there are no words on it
        next if values.length == 0
    
        # Increment the count for this array of words
        h[values] += 1
    end
    
    # For each key in the hash
    h.keys.each do |key|
        # Join the words in the array with a space and print the count
        puts "\"#{key.join(' ')}\" => #{h[key]}"
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am parsing a JBOSS log file using powershell. A typical line would being
I am parsing a log and would like an elegant way of calculating the
When parsing an xml file in android, I'm doing like this: try { InputStream
I have a problem parsing a XML file which contains special characters like ,
Perl newbie here. I have a log file that I need to parse out
i have a log file which contains hundreds/thousands of seperate XML messages and need
I'm writing a program that will parse an Apache log file periodically to log
I am working on a log parsing script using Perl. I am reading the
I'm parsing a log file to identify and retrieve information about failures. Regular Expressions
I am working on a log parsing program that retrieves the file to open

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.