I guess the simplest is to read the whole file in and process it by removing
backslash-newline sequences:
a=File.read(“a”).gsub!(/\\n/,‘’)
a=a.split(/\n/)
The split also removes the \n from each result line. Use
a=a.scan(/.*?\n/)
if you want to keep them (although that will miss the last line if the last
character of the file is not a newline)
You could perhaps also split the string on a regexp which is \n with a
look-behind assertion saying that it’s not preceeded by a backslash.
Personally I consider such things to be an abuse of regexps In any case,
that will leave the backslash-newline sequence within the result string,
which you didn’t want.
Also, the Pickaxe book doesn’t indicate that Ruby has look-behind assertions
(but it does have look-ahead assertions)
Regards,
Brian.
···
On Sun, Apr 27, 2003 at 09:39:00AM +0900, Jim Freeze wrote:
cat a
one
two
irb(main):001:0> a=File.read(“a”)
=> “one\\ntwo\n”
irb(main):002:0> b=File.readlines(“a”)
=> [“one\\n”, “two\n”]
class File
def each_cont (sep = $/)
h = ''
each_line (sep) { |x|
if x =~ /\#{sep}/
h << x.chomp("\#{sep}")
elsif h.empty?
yield (x)
else
yield (h+x)
h = ''
end
}
end
end
f = open (‘test.txt’).each_cont { |x| print x; }.close
I misinterpreted your request. I thought you wanted to use \ as the
line ending character. The following does what you want. Boundary
conditions (last line in file ends in , empty lines continued with ,
etc.) are left as an exercise for the reader
Regards,
JJ
#!/usr/bin/env ruby
def processLine(aLine)
p aLine
end
theLine = “”
File.open(ARGV[0], “r”) { |inFile|
inFile.each_line { |line|
line.chop!
if line =~ /^(.+)\$/
theLine << $1
next
else
theLine << line
end
processLine(theLine)
theLine = ""
}
}
···
On Sun, 2003-04-27 at 00:18, Jim Freeze wrote:
On Sunday, 27 April 2003 at 10:11:06 +0900, John Johnson wrote: