[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Read text file

Pragash Mr.

8/1/2008 10:16:00 AM

Hi,
I have one text file for example


9115 2008-06-20 07:26:35
91415 2008-06-20 07:26:32
9315 2008-06-20 07:26:33
2115 2008-06-20 07:26:05
15 2008-06-20 07:26:34
115 2008-06-20 07:26:35


i need to read only first column i.e numbers like 9115 etc if you have
any solution reply me
--
Posted via http://www.ruby-....

3 Answers

WujcioL

8/1/2008 10:29:00 AM

0

Pragash Mr. wrote:
> Hi,
> I have one text file for example
>
>
> 9115 2008-06-20 07:26:35
> 91415 2008-06-20 07:26:32
> 9315 2008-06-20 07:26:33
> 2115 2008-06-20 07:26:05
> 15 2008-06-20 07:26:34
> 115 2008-06-20 07:26:35
>
>
> i need to read only first column i.e numbers like 9115 etc if you have
> any solution reply me

Try this:

file = File.open("name.txt")
columns = []
file.each_line do |line|
columns << line.split(" ")[0]
end
p columns
--
Posted via http://www.ruby-....

Sebastian Hungerecker

8/1/2008 10:38:00 AM

0

Pragash Mr. wrote:
> 15 =C2=A02008-06-20 07:26:34
> 115 =C2=A02008-06-20 07:26:35
>
> i need to read only first column i.e numbers like 9115 etc if you have
> any solution reply me

numbers =3D File.open(filename) do |f|
f.map do |line|
line.to_i
end
end=20

HTH,
Sebastian
=2D-=20
Jabber: sepp2k@jabber.org
ICQ: 205544826

Andrés Suárez

8/1/2008 10:38:00 AM

0

Hi, there is another solution:

File.open('pp.txt') do |infile|
while line = infile.gets #the loop ends if infile.gets == EOF
line_split = line.split(" ") #split line through spaces; return
vector
puts line_split[0] #the first element of the line is the
number
end
end

for more information of this topic:

http://pleac.sourceforge.net/pleac_ruby/filea...
--
Posted via http://www.ruby-....