On 03/10/05, Kev Jackson <kevin.jackson / it.fts-vn.com> wrote:
>
> >
> > The typical Ruby idiom for this is:
> >
> > File.foreach(filename) do |line|
> >     line.chomp!
> >
> >     # ... processing code goes here ...
> > end
> >
> I didn't ry it to be honest, the real problem was that the data (when
> exported by another tool), has (\n) line endings at arbitrary places (ie
> 3 in a typical 'line' of data).  So I really wanted to strip them out
> before resplitting on the real line delimiter ';'.  I actually worked it
> out, I was trying filename.to_s.gsub!(/\n/, ''), but although ruby won't
> complain about this, it won't operate on it, if instead I spli on my
> delimiter ';' and then do a line.gsub!(/\n/,'') everything works fine
>
> Thanks for the help
>
> Is the idiom above more for processing multiple files (for each file,
> for each line do some processing close) kind of thing?
>
> Kev
>
>

filename.to_s.gsub!(/\n/, '') would replace all newlines in the
filename. That is certainly not what you want. You could do

string = File.read(filename).gsub(/\n/, '')

to get the content of the file without newlines

and

lines = File.read(filename).gsub(/\n/, '').split(";", -1)

to get what you want. The snippet from James is for processing each
line of a file after each other and may be a less memory intensive way
to achieve your goal.

hope to help,

Brian


--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/