T
T
TopClone2016-10-11 11:46:58
ruby
TopClone, 2016-10-11 11:46:58

How to fix the problem with opening a txt file?

This code easily opens the f.txt file and writes the keys to a separate array. The f.txt file contains no more than 10 lines.

c = Hash[*File.read('f.txt', encoding: "utf-8").split(/[:: :: \n]+/)]
n = c.each_key.to_a
puts n

But when I start working with a file of 70 thousand lines, an error appears in the console
tut.rb:13:in `[]': odd number of arguments for Hash (ArgumentError)
from tut.rb:13:in `'

Don't know what to do with it? Maybe there is some limit on the number of characters in the hash?
PS Thanks, I found what the problem is. It turned out that arguments including spaces break the whole picture. Not sure how to tell Ruby that let's say "All over again" is one argument?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2016-10-11
@alsopub

Most likely something is wrong with your file.
Perhaps there is a line with the wrong number of separators.
The error says - odd number argument for hash.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question