Extracting range of lines

P

Peña, Botp

From: tmarc [mailto:[email protected]]=20
# are there a ruby equivalents to perl's /regex/ .. /regex/ and /
# regex/ ... /regex/ idioms, examples described at
# http://www.unix.com.ua/orelly/perl/cookbook/ch06_09.htm?
# I've been googling around with no relevant results.

try searching for ruby+range+flip+flop

iianm, *that* perl/sed style is being deprecated.

eg,

File.open("test.txt") do |f|
while line=3Df.gets
p line
end
end
"--first\n"
"1234\n"
"456\n"
"4321\n"
"654\n"
"--second\n"
"546\n"
"3456\n"
"5436\n"
"9879\n"
"--third\n"
"1111\n"
#=3D> nil

File.open("test.txt") do |f|
while line=3Df.gets
p line if line =3D~ /^--second/ .. /^--third/
end
end
"--second\n"
#=3D> nil

so you get weird results.

the new ruby syntax is like,

File.open("test.txt") do |f|
while line=3Df.gets
p line if line =3D~ /^--second/ .. line =3D~ /^--third/
end
end
"--second\n"
"546\n"
"3456\n"
"5436\n"
"9879\n"
"--third\n"
#=3D> nil

hth.
kind regards -botp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top