splitting.............

H

Hunt Hunt

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
 
M

Mirko Viviani

something similar...

fi =3D File.open("yourfile", "r")

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

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

fi.close

puts allValues.inspect





2009/7/23 Hunt Hunt said:
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


--=20
-- Mirko Viviani --
GPG-PGP Public Key: 0xE4E8FAB1
Fingerprint: 14D3 A373 E926 7737 DF32 502B A4C4 1CE2 E4E8 FAB1
***********************************************
"=93Machines take me by surprise with great frequency.=94 A. Turing
 
R

Robert Schaaf

But what if your addressee is Samuel Barber at Capricorn, Mt. Kisko, =20
NY. Or in the Brill Building at 1619 Broadway, Suite 511, New York, =20
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, =20
Suite B",Kennenwick,WA

Other systems might escape internal commas. Worse, grabbing rows from =20=

a database and injecting commas between them, will lead to unparsable =20=

results.

There is no easy solution to this problem; you really have to parse on =20=

well-formed input.

Bob Schaaf


something similar...

fi =3D File.open("yourfile", "r")

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

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

fi.close

puts allValues.inspect





2009/7/23 Hunt Hunt said:
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 =20
associated
values.

how am i supposed to do this?

Thanks for the information in advance.

Regards
HUNT/RUDIN


--=20
-- Mirko Viviani --
GPG-PGP Public Key: 0xE4E8FAB1
Fingerprint: 14D3 A373 E926 7737 DF32 502B A4C4 1CE2 E4E8 FAB1
***********************************************
"=93Machines take me by surprise with great frequency.=94 A. Turing
 
H

Hunt Hunt

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 said:
At said:
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
 
S

Srikanth Jeeva

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
 
M

Mirko Viviani

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 said:
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 resul= ts.

There is no easy solution to this problem; you really have to parse on
well-formed input.

Bob Schaaf



On Jul 23, 2009, at 8:17 AM, Mirko Viviani wrote:

something similar...
fi =3D File.open("yourfile", "r")

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

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

fi.close

puts allValues.inspect





2009/7/23 Hunt Hunt <[email protected]>

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

--
-- Mirko Viviani --
GPG-PGP Public Key: 0xE4E8FAB1
Fingerprint: 14D3 A373 E926 7737 DF32 502B A4C4 1CE2 E4E8 FAB1
***********************************************
"=93Machines take me by surprise with great frequency.=94 A. Turing


--=20
-- Mirko Viviani --
GPG-PGP Public Key: 0xE4E8FAB1
Fingerprint: 14D3 A373 E926 7737 DF32 502B A4C4 1CE2 E4E8 FAB1
***********************************************
"=93Machines take me by surprise with great frequency.=94 A. Turing
 
H

Hunt Hunt

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 said:
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
 
S

Srikanth Jeeva

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
 
H

Hunt Hunt

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.
 
H

Hunt Hunt

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.
 
B

Brian Candler

Hunt said:
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
 
R

Robert Schaaf

Great! Someone's already done the work.

This should be in everyone's toolkit,

Bob Schaaf


Hunt said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top