How to read file content into array

Hello everyone
I want to read the file content into a array, using the following
coding, but it doesnot work.

This is the content of the file:
[“11.txt”, “11.txt”, “11.txt”, “11.txt”, “22.txt”, “11.txt”, “22.txt”,
“11.txt”]

I want to convert it into a array, using the following codes:
str_a=""
f = open(file_array_duplicate)

f.each_line { |line| str_a << line }
 f.close

a_s=str_a.gsub!(/[[]]/,’’).split(/\s*,\s*/)

but, the result is :
the array a_s content is:
:["[“11.txt”,", ““11.txt”,”, ““11.txt”,”, ““11.txt”,”,
““22.txt”,”, ““11.txt”,”, ““22.txt”,”, ““11.txt”]”]

Not that I wanted, likes
[“11.txt”, “11.txt”, “11.txt”, “11.txt”, “22.txt”, “11.txt”, “22.txt”,
“11.txt”]

Many thanks!

Well, the following works and prints
“11.txt”
“11.txt”
etc.

s=<<eos
[“11.txt”, “11,.txt”, “11.txt”, “11.txt”, “22.txt”, “11.txt”, “22.txt”,
“11.txt”]
eos
str=""
s.each_line{|line|str<<line}
a_s=str.gsub!(/[[]]/,’’).split(/\s*,\s*/)
a_s.each{|x|puts x}

However, this is not a proper way of parsing. Consider the following
input and it is going to fail:

[“11 , .txt”, “11.txt”, “11.txt”, “11.txt”, “22.txt”, “11.txt”,
“22.txt”,
“11.txt”]

If you trust the source unconditionally, try eval(string).

Alternatively, you can use Marshal to write variables to and read from a
file:

x = ['a , ‘, ‘"b’’]
File.write(“outfile”,Marshal.dump(x))
y = Marshal.load(File.read(“outfile”))

There is another option you can use to place lines of text into an
array. In the … The lines to examine are in blue; the rest you have
met before (get a file handle, loop round, use fgets to read the line).

http://www.onlinehackingtools.com/6-best-hacking-tool-for-twitter-website/

Hello: Dansei ,Swati
thank you, I resolved it by using a string method. thanks a lot.

Edward

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs