Splitting

Hi Friends,

I have a file which information.txt which is like

name,address,city,contact
abc,Newyork,HYU,345
xyz,Pittsurg,UYY,234

only theree line.

Now I have to split name, address,city & contact with their associated
values.

how am i supposed to do this?

Thanks for the information in advance.

Regards
HUNT/RUDIN

something similar…

fi = File.open(“yourfile”, “r”)

#get key values
header = fi.gets.chomp.split(",")
allValues = []

while (!fi.eof)
values = fi.gets.chomp.split(",")
allValues << header.zip(values)
end

fi.close

puts allValues.inspect

2009/7/23 Hunt H. [email protected]

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.

Bob Schaaf

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

hi Glenn
Let us suppose, an information.txt has following data


name,rollno,city,country
ABC,123,NewYork,US
XYZ,124,Pittsburg,US
LMN,125,Moscow,RUSSIA


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

Hi Hunt,

I don’t understand what is your need exactly.
Can u specify what is the output that u need??

Try This,

1.txt

name,rollno,city,country
ABC,123,NewYork,US
XYZ,124,Pittsburg,US
LMN,125,Moscow,RUSSIA


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


output :

[{“city”=>“NewYork”, “name”=>“ABC”, “country”=>“US”, “rollno”=>“123”},
{“city”=>“Pittsburg”, “name”=>“XYZ”, “country”=>“US”, “rollno”=>“124”},
{“city”=>“Moscow”, “name”=>“LMN”, “country”=>“RUSSIA”, “rollno”=>“125”}]

if this is not the format u needed, specify what output u need.

Thanks,
Srikanth J
ROR Developer, Chennai

http://srikanthjeeva.blogspot.com

hi Srikant,

1.txt


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

name=Alpha
rollno=123
city=Newyork
country=USA

In this file, ie 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.

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??

Try This,

1.txt

name,rollno,city,country
ABC,123,NewYork,US
XYZ,124,Pittsburg,US
LMN,125,Moscow,RUSSIA


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


output :

[{“city”=>“NewYork”, “name”=>“ABC”, “country”=>“US”, “rollno”=>“123”},
{“city”=>“Pittsburg”, “name”=>“XYZ”, “country”=>“US”, “rollno”=>“124”},
{“city”=>“Moscow”, “name”=>“LMN”, “country”=>“RUSSIA”, “rollno”=>“125”}]

if this is not the format u needed, specify what output u need.

Thanks,
Srikanth J
ROR Developer, Chennai

http://srikanthjeeva.blogspot.com

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 S. [email protected]

yes,
it is correct.

Actually, I am writing a program for practice.

class File
splitting which is done by you.
end
t1=File.find(“name”,“xyz”)
t1.name
t1.city
all this gives us name & city.

hi hunt,

Try this, this returns array of the names u asked for,

1.txt

name,rollno,city,country
ABC,123,NewYork,US
XYZ,124,Pittsburg,US
LMN,125,Moscow,RUSSIA


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

a.shift
b.shift
c.shift
d.shift

puts a.inspect
puts b.inspect
puts c.inspect
puts d.inspect
f.close


output:
[“ABC”, “XYZ”, “LMN”]
[“123”, “124”, “125”]
[“NewYork”, “Pittsburg”, “Moscow”]
[“US”, “US”, “RUSSIA”]

is this u need?

Thanks,
Srikanth J
ROR Developer, Chennai
http://srikanthjeeva.blogspot.com

Hunt H. wrote:

hi Srikant,

1.txt


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

name=Alpha
rollno=123
city=Newyork
country=USA

In this file, ie 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.

Now I think you may understand it.

I am new to ROR, just learning & learning.

Hugs
HUNT

Hunt H. wrote:

I have a file which information.txt which is like

name,address,city,contact
abc,Newyork,HYU,345
xyz,Pittsurg,UYY,234

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

that file 1.txt contains

name,contact_no,city,gender,more attributes…
xyz,2014567894,NY,M…

MNO,2014587965,Chicago,Female…
…,…,…,…,…,…

like wise we can have any number of attributes in file and its value

its like a table.

name contact city gender attribute1 attribute
2…attri n

M 890 NY Male value1 value2
value n

etc…

now I have created a class

class File
def self.find(name,args)
#i do splitting

instance_eval “define_method(key) { value}”

end

end

f1=File.find(“name”,“M”)
f1.name# this gives the name of the person.
f1.city #this gives the name of the city.

On Sat, Jul 25, 2009 at 11:20 AM, Robert S.[email protected]
wrote:

Great! Someone’s already done the work.

This should be in everyone’s toolkit

On Ruby 1.9, it is.

FasterCSV == The new CSV standard lib.

Great! Someone’s already done the work.

This should be in everyone’s toolkit,

Bob Schaaf

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