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 6643027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:00:00+00:00 2026-05-26T00:00:00+00:00

I am working on an assignment, and can’t figure it out. We have to

  • 0

I am working on an assignment, and can’t figure it out. We have to first parse a text file, and then feed the results into a hash. I have done this:

    code = File.open(WORKING_DIR + '/code.txt','r')
    char_count = {'a' => 0,'b' => 0,'c' => 0,'d' => 0,'e' => 0,'f' => 0,'g' => 0,'h' => 0,'i' => 0,
                   'j' => 0,'k' => 0,'l' => 0,'m' => 0,'n' => 0,'o' => 0,'p' => 0,'q' => 0,'r' => 0,
                   's' => 0,'t' => 0,'u' => 0,'v' => 0,'w' => 0,'x' => 0,'y' => 0,'z' => 0
                   }
# Step through each line in the file.
code.readlines.each do |line|

    # Print each character of this particular line.
    line.split('').each do
        |ch| 
            char_count.has_key?('ch')
                char_count['ch'] +=1        
           end

My line of thinking: open the file to a variable named code
read the individual lines
break the lines into each character.
I know this works, I can puts out the characters to screen.
Now I need to feed the characters into the hash, and it isn’t working. I am struggling with the syntax (at least) and basic concepts (at most). I only want the alphabet characters, not the punctuation, etc. from the file.

Any help would be greatly appreciated.

Thanks.

  • 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-05-26T00:00:01+00:00Added an answer on May 26, 2026 at 12:00 am

    I would directly do :

    File.open(WORKING_DIR + '/code.txt','r') do |f|
       char_count = Hash.new(0) # create a hash where 0 is the default value
       f.each_char do |c| # iterate on each character
          ... # some filter on the character you want to reject.
          char_count[c] +=1
       end
    end
    

    PS : you wrote 'ch' the string instead of ch the variable name

    EDIT : the filter could be

    f.each_char do |c| # iterate on each character
       next if c ~= \/W\ # exclude with a regexp non word character
       ....
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an assignment that is telling me to assume that I have
I am working on an assignment that deals with reading data from a text
Currently I'm working on an assignment and using C++ for the first time. I'm
I'm currently working on an assignment and this have had me stuck for hours.
I'm working on a new Moodle Assignment plugin. How can I include a custom
I am working on a short java assignment that I have been set. The
I am working on an assignment with polymorphism and have followed online tutorials that
I am working on a homework assignment where I have to implement a MergeSort
So I'm working on a research assignment with a gui and I have 3
Working on my assignment for Java and I have created a class called Triangle.

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.