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