File reading and line continuation with '\' 
Author Message
 File reading and line continuation with '\'

Hi:

Is there an simple way to get ruby to read lines in a file
ending with a backslash, '\', as a single line?

Quote:
> 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"]

I would like instead:

 f=File.readlines("a")
 ["onetwo\n"]

Thanks

--
Jim Freeze
----------
Positive, adj.:
        Mistaken at the top of one's voice.
                -- Ambrose Bierce, "The Devil's Dictionary"



Thu, 13 Oct 2005 08:39:00 GMT  
 File reading and line continuation with '\'
This should do it:

File.open(in_filename, "r") { |inFile|
  inFile.each_line ("\\") { |line|
        # whatever
  }

Quote:
}

Regards,
  JJ
Quote:

> Hi:

> Is there an simple way to get ruby to read lines in a file
> ending with a backslash, '\', as a single line?

> > 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"]

> I would like instead:

>  f=File.readlines("a")
>  ["onetwo\n"]

> Thanks

> --
> Jim Freeze
> ----------
> Positive, adj.:
>    Mistaken at the top of one's voice.
>            -- Ambrose Bierce, "The Devil's Dictionary"



Thu, 13 Oct 2005 09:11:06 GMT  
 File reading and line continuation with '\'

Quote:

> This should do it:

> File.open(in_filename, "r") { |inFile|
>   inFile.each_line ("\\") { |line|
>    # whatever
>   }
> }

Hmm... I don't get it.

irb(main):009:0> File.open("a") { |f|
irb(main):010:1* f.each_line("\\") { |line| p line }}
"one \\"
"\ntwo \\"
"\nthree\nfour\n"
=> #<File:0x1de408>

Quote:
> cat a

one \
two \
three
four

I was thinking I would get:

"one two three\n"
"three\n"
"four"

ri says:
----------------------------------------------------------- IO#each_line
  ios.each_line( aSepString=$/ ) {| line | block }  -> ios
------------------------------------------------------------------------
  Synonym for IO#each.

What I need to match on as a sep string is /[^\\]\n$/.
I don't see how "\\" can work, but, it is after midnight
my time.

Jim

Quote:
> Regards,
>   JJ


> > Hi:

> > Is there an simple way to get ruby to read lines in a file
> > ending with a backslash, '\', as a single line?

> > > 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"]

> > I would like instead:

> >  f=File.readlines("a")
> >  ["onetwo\n"]

> > Thanks

> > --
> > Jim Freeze
> > ----------
> > Positive, adj.:
> >       Mistaken at the top of one's voice.
> >               -- Ambrose Bierce, "The Devil's Dictionary"

--
Jim Freeze
----------
Acquaintance, n.:
        A person whom we know well enough to borrow from, but not well
enough to lend to.
                -- Ambrose Bierce, "The Devil's Dictionary"


Thu, 13 Oct 2005 12:18:31 GMT  
 File reading and line continuation with '\'

Quote:

> > 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"]

> I would like instead:

>  f=File.readlines("a")
>  ["onetwo\n"]

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.



Thu, 13 Oct 2005 18:10:53 GMT  
 File reading and line continuation with '\'

Quote:
> > > 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"]

> > I would like instead:

> >  f=File.readlines("a")
> >  ["onetwo\n"]

Alternative solution: how about joining the adjacent strings together
in-place after reading them in?

lastline = ""
a.delete_if do |line|
  if lastline.gsub!(/\\\n\z/,line)
    true
  else
    lastline = line
    false
  end
end

which can be even further obfuscated to:

lastline = ""
a.delete_if do |line|
  lastline.gsub!(/\\\n\z/,line) or not lastline = line
end

# before: a=["one\n", "two\\\n", "three\\\n", "four\n", "five\n"]
# after:  a=["one\n", "twothreefour\n", "five\n"]

Regards,

Brian.



Thu, 13 Oct 2005 18:35:37 GMT  
 File reading and line continuation with '\'

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  
--

saluti,
Marjan Popovski



Fri, 14 Oct 2005 11:23:59 GMT  
 File reading and line continuation with '\'
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 = ""
  }

Quote:
}


> > This should do it:

> > File.open(in_filename, "r") { |inFile|
> >   inFile.each_line ("\\") { |line|
> >       # whatever
> >   }
> > }

> Hmm... I don't get it.

> irb(main):009:0> File.open("a") { |f|
> irb(main):010:1* f.each_line("\\") { |line| p line }}
> "one \\"
> "\ntwo \\"
> "\nthree\nfour\n"
> => #<File:0x1de408>

> > cat a
> one \
> two \
> three
> four

> I was thinking I would get:

> "one two three\n"
> "three\n"
> "four"

> ri says:
> ----------------------------------------------------------- IO#each_line
>   ios.each_line( aSepString=$/ ) {| line | block }  -> ios
> ------------------------------------------------------------------------
>   Synonym for IO#each.

> What I need to match on as a sep string is /[^\\]\n$/.
> I don't see how "\\" can work, but, it is after midnight
> my time.

> Jim

> > Regards,
> >   JJ


> > > Hi:

> > > Is there an simple way to get ruby to read lines in a file
> > > ending with a backslash, '\', as a single line?

> > > > 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"]

> > > I would like instead:

> > >  f=File.readlines("a")
> > >  ["onetwo\n"]

> > > Thanks

> > > --
> > > Jim Freeze
> > > ----------
> > > Positive, adj.:
> > >  Mistaken at the top of one's voice.
> > >          -- Ambrose Bierce, "The Devil's Dictionary"

> --
> Jim Freeze
> ----------
> Acquaintance, n.:
>    A person whom we know well enough to borrow from, but not well
> enough to lend to.
>            -- Ambrose Bierce, "The Devil's Dictionary"



Fri, 14 Oct 2005 23:45:20 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. what's about line continuation in TASM ?

2. Problem with 39 continuation lines

3. 'comment lines' in data files

4. File Reading in 'J'

5. 'wav' files reading

6. Opening a file with ACTION='READ'

7. File Object -- function 'read'

8. Reading unformatted files 'bit a bit'

9. Reading fortran 'unformatted' binary files

10. reading 'stream_lz' data files

11. stream('file','c','seek ='x) problem

12. Break line across lines (not in '[]' or '{}')?

 

 
Powered by phpBB® Forum Software