--
-- Mirko Viviani --
GPG-PGP Public Key: 0xE4E8FAB1
Fingerprint: 14D3 A373 E926 7737 DF32 502B A4C4 1CE2 E4E8 FAB1
***********************************************
"“Machines take me by surprise with great frequency.” A. Turing
What do you want the resulting data structure to look like?
This creates an array of hashes, and then a hash of arrays:
filename = 'information.txt'
f = File.open(filename)
fields = f.gets.chomp.split(/,/)
data =
while line = f.gets
line.chomp!
data << Hash[fields.zip(line.split(/,/))]
end
p data
f.rewind
fields = f.gets.chomp.split(/,/)
data = Hash.new()
while line = f.gets
line.chomp.split(/,/).each_with_index do |item, idx|
data[fields[idx]] += [item]
end
end
p data
f.close
Or, take Robert Schaaf's advice and parse the file with CSV if you don't
control how it's created:
require 'csv'
csv = CSV.open(filename, 'r')
fields = csv.shift
data =
# assume no empty lines in the file
until (row = csv.shift).empty?
data << Hash[fields.zip(row)]
end
csv.close
p data
require 'rubygems'
require 'fastercsv'
FasterCSV.foreach("information.txt", :headers=>true) do |row|
p row['name']
p row['address']
p row['city']
p row['contact']
end
But what if your addressee is Samuel Barber at Capricorn, Mt. Kisko, NY. Or in the Brill Building at 1619 Broadway, Suite 511, New York, NY? Or a law firm such as Miller, Mertens & Comfort, P.L.L.C. ?
Excel will export these as
Samuel Barber,"Capricorn, Mt. Kisco",NY
Tin Pan Alley,"1619 Broadway, Suite 511", New York, NY
"Miller, Mertens & Comfort, P.L.L.C.","1020 North Center Parkway, Suite B",Kennenwick,WA
Other systems might escape internal commas. Worse, grabbing rows from a database and injecting commas between them, will lead to unparsable results.
There is no easy solution to this problem; you really have to parse on well-formed input.
--
-- Mirko Viviani --
GPG-PGP Public Key: 0xE4E8FAB1
Fingerprint: 14D3 A373 E926 7737 DF32 502B A4C4 1CE2 E4E8 FAB1
***********************************************
"“Machines take me by surprise with great frequency.” A. Turing
Now I have to split name, address,city & contact with their associated
values.
how am i supposed to do this?
What do you want the resulting data structure to look like?
This creates an array of hashes, and then a hash of arrays:
filename = 'information.txt'
f = File.open(filename)
fields = f.gets.chomp.split(/,/)
data =
while line = f.gets
line.chomp!
data << Hash[fields.zip(line.split(/,/))]
end
p data
f.rewind
fields = f.gets.chomp.split(/,/)
data = Hash.new()
while line = f.gets
line.chomp.split(/,/).each_with_index do |item, idx|
data[fields[idx]] += [item]
end
end
p data
f.close
Or, take Robert Schaaf's advice and parse the file with CSV if you don't
control how it's created:
require 'csv'
csv = CSV.open(filename, 'r')
fields = csv.shift
data =
# assume no empty lines in the file
until (row = csv.shift).empty?
data << Hash[fields.zip(row)]
end
csv.close
p data
require 'rubygems'
require 'fastercsv'
FasterCSV.foreach("information.txt", :headers=>true) do |row|
p row['name']
p row['address']
p row['city']
p row['contact']
end
--
Posted via http://www.ruby-forum.com/\.
f = File.open('1.txt')
a=[]
c = []
b={}
d = []
f.readlines.each do |lines|
line_arr = lines.strip.split(',')
a << line_arr
end
c << a.shift
c = c.flatten
a.each do |x|
b[c[0]] = x[0]
b[c[1]] = x[1]
b[c[2]] = x[2]
b[c[3]]= x[3]
d << b
b = {}
end
puts d.inspect
f.close
Just change the "," as record delimiter.... for example try a "\t" (TAB).
You can easily change this through Excel if you export your file as CSV
···
2009/7/23 Robert Schaaf <ruby-bucket@comcast.net>
But what if your addressee is Samuel Barber at Capricorn, Mt. Kisko, NY.
Or in the Brill Building at 1619 Broadway, Suite 511, New York, NY? Or a
law firm such as Miller, Mertens & Comfort, P.L.L.C. ?
Excel will export these as
Samuel Barber,"Capricorn, Mt. Kisco",NY
Tin Pan Alley,"1619 Broadway, Suite 511", New York, NY
"Miller, Mertens & Comfort, P.L.L.C.","1020 North Center Parkway, Suite
B",Kennenwick,WA
Other systems might escape internal commas. Worse, grabbing rows from a
database and injecting commas between them, will lead to unparsable results.
There is no easy solution to this problem; you really have to parse on
well-formed input.
--
-- Mirko Viviani --
GPG-PGP Public Key: 0xE4E8FAB1
Fingerprint: 14D3 A373 E926 7737 DF32 502B A4C4 1CE2 E4E8 FAB1
***********************************************
"“Machines take me by surprise with great frequency.” A. Turing
--
-- Mirko Viviani --
GPG-PGP Public Key: 0xE4E8FAB1
Fingerprint: 14D3 A373 E926 7737 DF32 502B A4C4 1CE2 E4E8 FAB1
***********************************************
"“Machines take me by surprise with great frequency.” A. Turing
f = File.open('1.txt')
a=
b=
c=
d=
f.readlines.each do |lines|
line_arr = lines.split(',')
a << line_arr[0].strip
b << line_arr[1].strip
c << line_arr[2].strip
d << line_arr[3].strip
end
name,rollno,city,country
ABC,123,NewYork,US
XYZ,124,Pittsburg,US
LMN,125,Moscow,RUSSIA
--------------------------------------------------
let us suppose there is a file 4.txt
I can split it by
--------------------------------------
File.open('4.txt','r') do |file|
file.each_line do | line |
key, value = line.split('=')
end
end
--------------------------------------
I have key as name and its value as ALpha
again I have key as rollno and value as 123.
but in the 1.txt file.data are not organised in this manner, so how can
I split it.
or how can i make an array of name which contains all the name.
how can i make an array of roll no with name as rollno.