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 Jeeva 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
--
Posted via http://www.ruby-forum.com/.