How to define a record delimitter for IO.foreach

Ruby experts:

Another newbie question:

I use this code to step through lines in a text file and reading tab-delimitted fields:

IO.foreach("./control/packages.txt") { |x|
field = x.chop.split(’\t’)

By default, this code assumes that records are delimted by a hard return (or end-of-line) character. QUESTION: Is there anyway I can specify an alternative record delimitter, such as a series of characters, so that each record could span two or more lines?

Thanks.

-Kurt

Hello –

···

On Wed, 7 Aug 2002, Kurt Euler wrote:

Ruby experts:

Another newbie question:

I use this code to step through lines in a text file and reading tab-delimitted fields:

IO.foreach(“./control/packages.txt”) { |x|
field = x.chop.split(‘\t’)

By default, this code assumes that records are delimted by a hard
return (or end-of-line) character. QUESTION: Is there anyway I can
specify an alternative record delimitter, such as a series of
characters, so that each record could span two or more lines?

Change the value of the global variable $/ .

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hi Kurt,

You might want to take a look at IO.readlines. Here is the method signature:
IO.readlines( filename, sepString=$/ ) → anArray

which means this should probably do the trick.
IO.readlines(‘./control/packages.txt’, “\t”).each do |line|

end


Signed,
Holden Glova

···

On Wed, 07 Aug 2002 09:15, Kurt Euler wrote:

Ruby experts:

Another newbie question:

I use this code to step through lines in a text file and reading
tab-delimitted fields:

IO.foreach(“./control/packages.txt”) { |x|
field = x.chop.split(‘\t’)

By default, this code assumes that records are delimted by a hard return
(or end-of-line) character. QUESTION: Is there anyway I can specify an
alternative record delimitter, such as a series of characters, so that each
record could span two or more lines?

Thanks.

-Kurt