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.
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 S.'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
Now how can i have name ABC,XYZ & LMN with name.
123,124,125 with rollno
NewYork, Pittsburg, Moscow with city
US, US, Russia with country.
Glenn J. wrote:
At 2009-07-23 08:06AM, “Hunt H.” wrote:
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 S.'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
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
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.
Now I think you may understand it.
I am new to ROR, just learning & learning.
Hugs
HUNT
Srikanth J. wrote:
Hi Hunt,
I don’t understand what is your need exactly.
Can u specify what is the output that u need??
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
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
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.
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