/BEGIN/ .. /END/ file reading

hello

i know that it works,
but not how :slight_smile:

cat vcard | ruby parse.rb

while vblk = ( /BEGIN:VCARD/ … /END:VCARD/ )

end

i whould need a hint.

thanks

best regards
karl-heinz

“Wild Karl-Heinz” kh.wild@wicom.li schrieb im Newsbeitrag
news:71430124625.20030616133402@wicom.li…

hello

i know that it works,
but not how :slight_smile:

cat vcard | ruby parse.rb

while vblk = ( /BEGIN:VCARD/ … /END:VCARD/ )

end

i whould need a hint.

A range operator with a regexp works like a flip flop (bistable multi
vibrator or so). The value of the internal flag (and thus the result of
the evaluation) changes in this way: initiallly it’s false. If the first
RE matches it’s switched to true. It stays true as long as the second RE
does not match. If it does, the flag goes back to false.

while( line = gets )
if /BEGIN:VCARD/ =~ line … /END:VCARD/ =~ line
puts line
end
end

Regards

robert

If you’re looking for a vcard decoder/encoder, I wrote one, and you can find
it on the RAA:

http://raa.ruby-lang.org/list.rhtml?name=vcard

It will decode a file into an array of vcards, and deal with things like
the begin: and end: are case-insensitive, and there can be whitespace
around the tokens.

Cheers,
Sam

Quoteing kh.wild@wicom.li, on Mon, Jun 16, 2003 at 08:34:14PM +0900:

···

hello

i know that it works,
but not how :slight_smile:

cat vcard | ruby parse.rb

while vblk = ( /BEGIN:VCARD/ … /END:VCARD/ )

end

i whould need a hint.

thanks

best regards
karl-heinz

A range operator with a regexp works like a flip flop (bistable
multi
vibrator or so). The value of the internal flag (and thus the
result of
the evaluation) changes in this way: initiallly it’s false. If the
first
RE matches it’s switched to true. It stays true as long as the
second RE
does not match. If it does, the flag goes back to false.

while( line = gets )
if /BEGIN:VCARD/ =~ line … /END:VCARD/ =~ line
puts line
end
end

Did perl originate this idea? I remember way back when in perl 4.0x
days when I figured out how that thing worked… it was one of those
“a-HA!” moments.

Cool operator.

···

Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!

A range operator with a regexp works like a flip flop (bistable
multi
vibrator or so). The value of the internal flag (and thus the
result of
the evaluation) changes in this way: initiallly it’s false. If the
first
RE matches it’s switched to true. It stays true as long as the
second RE
does not match. If it does, the flag goes back to false.

while( line = gets )
if /BEGIN:VCARD/ =~ line … /END:VCARD/ =~ line

I don’t get this syntax. Isn’t the last line equivalent to:
if (/BEGIN:VCARD/ =~ line) … (/END:VCARD/ =~ line)

which creates at each iteration a new range object, which knows nothing
about previous lines or state of the switch.

[gus@comp tests]$ irb -v
irb 0.7.4(01/05/08)
[gus@comp tests]$ irb
irb(main):001:0> r = (/BEGIN:VCARD/ =~ line) … (/END:VCARD/ =~ line)
NameError: undefined local variable or method `line’ for
#Object:0x353ce0
from (irb):1
irb(main):002:0> line = “toto”
“toto”
irb(main):003:0> r = (/BEGIN:VCARD/ =~ line) … (/END:VCARD/ =~ line)
ArgumentError: bad value for range
from (irb):3

Why does line 3 crashes? What’s the difference with your code above,
when inside the if statement?

Your piece of code works as explained but I can’t make sense out of it.
Can you shed some light on this please?

Guillaume.

···

On Mon, 2003-06-16 at 09:40, Michael Campbell wrote:

puts line

end
end

Did perl originate this idea? I remember way back when in perl 4.0x
days when I figured out how that thing worked… it was one of those
“a-HA!” moments.

Cool operator.


Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

Michael Campbell michael_s_campbell@yahoo.com writes:

Did perl originate this idea? I remember way back when in perl 4.0x
days when I figured out how that thing worked… it was one of those
“a-HA!” moments.

Perl may have borrowed it from sed, which supports a two address form
in many commands.

From sed

A command line with two addresses selects the inclusive range from
the first pattern space that matches the first address to the next
pattern space that matches the second. (If the second address is a
number less than or equal to the line number first selected, only
one line will be selected.) Starting at the first line following the
selected range, sed looks again for the first address. Thereafter
the process is repeated.

Addresses are separated by commas and can be either regular
expressions, line numbers or “$”.

I don’t know where sed borrowed it from.

when inside the if statement?

You have found : when ruby is in a condition (if, unless, while, ...) it
transform a range operator into a flip-flop operator.

Guy Decoux

“Guillaume Marcais” guslist@free.fr schrieb im Newsbeitrag
news:1055774509.12403.53.camel@comp…

A range operator with a regexp works like a flip flop (bistable
multi
vibrator or so). The value of the internal flag (and thus the
result of
the evaluation) changes in this way: initiallly it’s false. If the
first
RE matches it’s switched to true. It stays true as long as the
second RE
does not match. If it does, the flag goes back to false.

while( line = gets )
if /BEGIN:VCARD/ =~ line … /END:VCARD/ =~ line

I don’t get this syntax. Isn’t the last line equivalent to:
if (/BEGIN:VCARD/ =~ line) … (/END:VCARD/ =~ line)

No, it’s not. Your code first evaluates the regexps and then creates an
integer range! My code considered the whole thing and creates only a
single range instance. I guess there’s a little bit of magic in the
parsing (i.e. a range with regexp will be treated differently from another
range).

which creates at each iteration a new range object, which knows nothing
about previous lines or state of the switch.

That’s exactly the reason why it is not the same. :slight_smile:

[gus@comp tests]$ irb -v
irb 0.7.4(01/05/08)
[gus@comp tests]$ irb
irb(main):001:0> r = (/BEGIN:VCARD/ =~ line) … (/END:VCARD/ =~ line)
NameError: undefined local variable or method `line’ for
#Object:0x353ce0
from (irb):1
irb(main):002:0> line = “toto”
“toto”
irb(main):003:0> r = (/BEGIN:VCARD/ =~ line) … (/END:VCARD/ =~ line)
ArgumentError: bad value for range
from (irb):3

Why does line 3 crashes? What’s the difference with your code above,
when inside the if statement?

It’s not the if but the brackets.

Your piece of code works as explained but I can’t make sense out of it.
Can you shed some light on this please?

Did that help?

robert
···

On Mon, 2003-06-16 at 09:40, Michael Campbell wrote:

xalapax@yahoo.com schrieb im Newsbeitrag news:un0ghz8qw.fsf@yahoo.com

Michael Campbell michael_s_campbell@yahoo.com writes:

Perl may have borrowed it from sed, which supports a two address form
in many commands.

I don’t know where sed borrowed it from.

Nowhere. Un*x tools are always original. :-))

At least that’s what I guess.

robert

I’ve to correct me. It’s not the brackets but the if as Guy Decoux said.

robert

What is the life span of the flip-flop “object” (is it an object?)
created? Obviously in the code sample it outlived the if statement. But
the following (contrived) piece code doesn’t work:

def test(s)
if /^b/ =~ s … /^e/ =~ s
puts s
end
end
while (line = gets) do
test(line)
end

So does it have a scope similar to a local variable?

Guillaume.

···

On Mon, 2003-06-16 at 10:55, ts wrote:

when inside the if statement?

You have found : when ruby is in a condition (if, unless, while, …) it
transform a range operator into a flip-flop operator.

Guy Decoux

dear devels

why cant we just use the /* */ style of multi line commenting

···

On Tue, 17 Jun 2003 17:43:06 +0900 “Robert Klemme” bob.news@gmx.net wrote:

xalapax@yahoo.com schrieb im Newsbeitrag news:un0ghz8qw.fsf@yahoo.com

Michael Campbell michael_s_campbell@yahoo.com writes:

Perl may have borrowed it from sed, which supports a two address form
in many commands.

I don’t know where sed borrowed it from.

Nowhere. Un*x tools are always original. :-))

At least that’s what I guess.

robert


Regards,
Warren Brian Noronha.
warren@freedomink.org
http://warren.freedomink.org

Key fingerprint = 4B76 48B0 3612 986E 519A 1279 BDDA 9236 0139 471B

So does it have a scope similar to a local variable?

Yes, it use internally a local variable to remember the state

Guy Decoux

So does it have a scope similar to a local variable?

Yes - in fact it’s implemented internally using a hidden local
variable. See ‘case NODE_FLIP2’ in rb_eval in eval.c

– George

Thanks, I learned a new feature of ruby. Always exiting.

Guillaume.

···

On Tue, 2003-06-17 at 04:21, George Marrows wrote:

So does it have a scope similar to a local variable?

Yes - in fact it’s implemented internally using a hidden local
variable. See ‘case NODE_FLIP2’ in rb_eval in eval.c

– George