Reading past a file header

Looking for a more idiomatic way to do this:

flag = false
IO.foreach(file) {|line|
flag = true if line =~ /^–begin–$/
next unless flag

}

I vaguely remember something about using a range of regexps to do this
without the flag variable, but I can’t find the exact method.

martin

  flag = true if line =~ /^--begin--$/

     next if 1 .. line =~ /^--begin--$/

it will begin the processing *after* the line /^--begin--$/

Guy Decoux

Thanks! The line after --begin-- is precisely what I need.

martin

···

ts decoux@moulon.inra.fr wrote:

flag = true if line =~ /^–begin–$/

next if 1 .. line =~ /^--begin--$/

it will begin the processing after the line /^–begin–$/

Didn’t work when I tried it - everytmie the regex reset, 1 was true
again, so it never processed the file. This worked:

next if ($. == 1) … (line =~ /^–begin–$/)

martin

···

ts decoux@moulon.inra.fr wrote:

flag = true if line =~ /^–begin–$/

next if 1 .. line =~ /^--begin--$/

it will begin the processing after the line /^–begin–$/

This looks like confusing code. Care to put it in context and give us
a 2 minute tutorial?

Gavin

···

On Friday, January 31, 2003, 4:02:44 AM, Martin wrote:

ts decoux@moulon.inra.fr wrote:

flag = true if line =~ /^–begin–$/

next if 1 .. line =~ /^--begin--$/

it will begin the processing after the line /^–begin–$/

Thanks! The line after --begin-- is precisely what I need.

next if ($. == 1) .. (line =~ /^--begin--$/)

pigeon% cat b.rb
#!/usr/bin/ruby -v
IO.foreach('b.rb') do |line|
   next if 1 .. /^__END__$/ =~ line
   p line
end
__END__
Some text
after __END__
pigeon%

pigeon% b.rb
ruby 1.6.8 (2002-12-24) [i686-linux]
"Some text\n"
"after __END__\n"
pigeon%

Guy Decoux

Pickaxe, 224 [Ranges in Boolean Expressions]

=> a…b used in a boolean expression means the following:

  • a is evaluated; if true, set “internal state” to true
  • b is evaluated; if true, set “internal state” to false
  • if “internal state” is true, it remains so until b becomes true

so
1 … line =~ re
behaves like
true … line =~ re
which is true until ‘line =~ re’

···

On Fri, Jan 31, 2003 at 10:31:32AM +0900, Gavin Sinclair wrote:

On Friday, January 31, 2003, 4:02:44 AM, Martin wrote:

ts decoux@moulon.inra.fr wrote:

flag = true if line =~ /^–begin–$/

next if 1 .. line =~ /^--begin--$/

it will begin the processing after the line /^–begin–$/

Thanks! The line after --begin-- is precisely what I need.

This looks like confusing code. Care to put it in context and give us
a 2 minute tutorial?


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Never trust an operating system you don’t have sources for. :wink:
– Unknown source

Sure. Say you have a file that is divided into sections, demarcated by
section breaks. Here’s an example:
$ cat testfile
precomments
–begin–
data
–section–
more data
–section–
still more data
–end–
postcomments

Now, when reading in the file via IO.foreach, we want to skip the
precomments, and start reading at --begin–. From the Pickaxe book:

You can use a Ruby range as a boolean expression. A range such as
exp1…exp2 will evaluate as false until exp1 becomes true. The range
will then evaluate as true until exp2 becomes true. Once this happens,
the range resets, ready to fire again.

So, to construct a range that is true whe the file starts, we use
($. == 1) [where $. is the file counter], and have it return true until
we reach our begin statement. Here’s the code for reading the above
file:

$ cat test.rb
i = 0
IO.foreach(‘testfile’) {|line|
next if ($. == 1)…(line =~ /^–begin–$/)
case line
when /^–section–$/
i+=1; next
when /^–end–$/
break
end
puts “section #{i}: #{line}”
}

$ ruby test.rb
section 0: data
section 1: more data
section 2: still more data

martin

···

Gavin Sinclair gsinclair@soyabean.com.au wrote:

On Friday, January 31, 2003, 4:02:44 AM, Martin wrote:

ts decoux@moulon.inra.fr wrote:

flag = true if line =~ /^–begin–$/

next if 1 .. line =~ /^--begin--$/

it will begin the processing after the line /^–begin–$/

Thanks! The line after --begin-- is precisely what I need.

This looks like confusing code. Care to put it in context and give us
a 3 minute tutorial?

$ ruby -v
ruby 1.7.3 (2002-11-17) [i386-mswin32]

$ ruby b.rb

$ /c/ruby1.6/bin/ruby b.rb
“Some text\n”
“after END\n”
“\n”

martin

···

ts decoux@moulon.inra.fr wrote:

next if ($. == 1) … (line =~ /^–begin–$/)

pigeon% cat b.rb
#!/usr/bin/ruby -v
IO.foreach(‘b.rb’) do |line|
next if 1 … /^END$/ =~ line
p line
end
END
Some text
after END

so
  1 .. line =~ re
behaves like
  true .. line =~ re

This is in reality

       $. == 1 .. line =~ re

it's probably in the category

    * discourage use of Perlish features by giving warnings.

:slight_smile:

Guy Decoux

$ ruby -v
ruby 1.7.3 (2002-11-17) [i386-mswin32]

see my remark in [ruby-talk:63303] :-)))

Guy Decoux

Ah - that hadn’t made it onto my newsserver yet.

I guess that’s one perlish feature that didn’t make the cut :slight_smile:

martin

···

ts decoux@moulon.inra.fr wrote:

$ ruby -v
ruby 1.7.3 (2002-11-17) [i386-mswin32]

see my remark in [ruby-talk:63303] :-)))