From: Holden Glova Date: 2002-01-19T06:41:32+09:00 Subject: Re: Hello! Array sub classing? -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Sat, 19 Jan 2002 10:12, Markt wrote: > TABfile = "TAB14.txt" > TABf = open(TABfile, "r")lines = TABf.readline > lines = lines#split(/\n/) > print lines[2] > > I think the trouble comes as the data has text delimited by tabs, but the > text is not enclosed by ". > > So the view of the data is of more elements than I want. > > So what s the code for reading a line delimeted by a tab? > > I've tried the obvious... just need that \t in the right place... > > Regards & thanks. > > Markt Markt, I'm not sure this is what you want, but I'll take a stab at it. synack@eugene:~/tmp$ cat > tab.txt 1 2 3 4 5 6 7 8 9 Now there is a file with tab delimeted values. synack@eugene:~/tmp$ cat > t.rb IO.foreach('tab.txt') do |line| puts "A line..." puts line.split(/\t/) end IO is the superclass of File. You can use the foreach method to go through each line in the file, by default the delimeter is set to newline ($/). Once IO#foreach is completed the file is automatically closed. Keep in mind that errors can be caused as with any IO so you might want to resuce yourself from those situations... Here is the output from the above. synack@eugene:~/tmp$ ruby t.rb A line... 1 2 3 A line... 4 5 6 A line... 7 8 9 Hope this helps. - -- Signed, Holden Glova -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQE8SJc/+mF116Lw2cQRAh0eAJ9tRhuRAJN8t44fPrM+rhi8BIo+xACfb+w1 9ARohQhWraqQqTza/D6qgIM= =BX0O -----END PGP SIGNATURE-----