Please tell me how to resolve the error of the program that counts numbers in ruby.

Asked 2 years ago, Updated 2 years ago, 74 views

The code and error statements for the program are listed below.Please tell me how to resolve the error.Also, since I am using a csv file, the file is shown below.Thank you for your cooperation.

I am writing a ruby program like this

require'csv'
    hash = Hash.new
    File.foreach('sample.csv') do | row |
      col=row.chomp.split(",")
      if col.length>4
        for in 3..col.length-1
          txt+=col[i]+", "
        end
      end
      if hash {:col[2]} == nil
        hash {:col[2]}=1
      else
        hash {:col[2]}+=1
      end
    end
    putshash.keys

error message

prog$ruby freq.rb
freq.rb:12:syntax error, unexpected '=', expecting kEND
    hash {:col[2]}=1
                   ^
freq.rb:14:syntax error, unexpected tOP_ASGN, expecting kEND
    hash {:col[2]}+=1
                    ^
prog$

CSV file

6,2,4, "<One option would be to clone your startup drive to an external disk using something like SuperDuper! or Carbon Copy Cloner.Then you can use System Preferences->Startup Disk to select that external drive as the drive;lt;drove.

<p>Once you've rebooted and are running the system off the external drive you can use Disk Utility to run the repair. After you'redone, re-select the internal drive as the Startup Disk and reboot.</p>"
(Omitted)
27, 2, 12, "<p>They are not really" in memory, "more like cached to disk if and when necessary. Many apps don't use the multi-tasking or area't setup for it. When you switch it does actually close the app.</pt;

<Being in the task bar does not 't guarantee that's actually in memory, active running, or both.The OS managements that.</p>"

ruby csv

2022-09-30 13:59

1 Answers

It's a grammatical problem.The Hash value is referenced incorrectly.
If hash{:col[2]} is hash[col[2]], the error will be resolved.

In addition, I was concerned that require'csv', but I did not use any CSV functionality and analyzed CSV format on my own.
For example, you can read one record at a time from a CSV file:

CSV.foreach('sample.csv') do | row |
  ...
end

Escape "" is also handled properly, so it's easy.


2022-09-30 13:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.