/BEGIN/ .. /END/ file reading 
Author Message
 /BEGIN/ .. /END/ file reading

hello

i know that it works,
but not how :-)

cat vcard | ruby parse.rb

while vblk = ( /BEGIN:VCARD/ .. /END:VCARD/ )
    ...
end

i whould need a hint.

thanks

best regards
karl-heinz



Fri, 02 Dec 2005 19:34:14 GMT  
 /BEGIN/ .. /END/ file reading



Quote:
> hello

> i know that it works,
> but not how :-)

> 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
{*filter*} 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



Fri, 02 Dec 2005 21:18:53 GMT  
 /BEGIN/ .. /END/ file reading

Quote:
> A range operator with a regexp works like a flip flop (bistable
> multi
> {*filter*} 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!
http://www.*-*-*.com/



Fri, 02 Dec 2005 21:40:01 GMT  
 /BEGIN/ .. /END/ file reading

Quote:

> > A range operator with a regexp works like a flip flop (bistable
> > multi
> > {*filter*} 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.


irb 0.7.4(01/05/08)

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.

Quote:
> >     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://www.*-*-*.com/



Fri, 02 Dec 2005 22:43:32 GMT  
 /BEGIN/ .. /END/ file reading

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



Fri, 02 Dec 2005 22:55:15 GMT  
 /BEGIN/ .. /END/ file reading



Quote:

> > > A range operator with a regexp works like a flip flop (bistable
> > > multi
> > > {*filter*} 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).

Quote:
> 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. :-)

Quote:

> irb 0.7.4(01/05/08)

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

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



Fri, 02 Dec 2005 23:00:25 GMT  
 /BEGIN/ .. /END/ file reading

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

    robert



Fri, 02 Dec 2005 23:03:21 GMT  
 /BEGIN/ .. /END/ file reading
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.

Quote:


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



Sat, 03 Dec 2005 01:20:20 GMT  
 /BEGIN/ .. /END/ file reading

Quote:

> 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 http://www.opengroup.org/onlinepubs/7908799/xcu/sed.html

  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.



Sat, 03 Dec 2005 11:24:39 GMT  
 /BEGIN/ .. /END/ file reading

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



Sat, 03 Dec 2005 16:02:01 GMT  
 /BEGIN/ .. /END/ file reading

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

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

Guy Decoux



Sat, 03 Dec 2005 16:14:52 GMT  
 /BEGIN/ .. /END/ file reading


Quote:

> 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



Sat, 03 Dec 2005 15:33:51 GMT  
 /BEGIN/ .. /END/ file reading

Quote:


> > 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. :-))

sco.  (sorry, cant resist :-)
Quote:

> At least that's what I guess.

>     robert



Sat, 03 Dec 2005 16:46:56 GMT  
 /BEGIN/ .. /END/ file reading
Thanks, I learned a new feature of ruby. Always exiting.

Guillaume.

Quote:

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



Sat, 03 Dec 2005 21:44:17 GMT  
 /BEGIN/ .. /END/ file reading

dear devels

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

On Tue, 17 Jun 2003 17:43:06 +0900

|


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

http://warren.freedomink.org

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

  application_pgp-signature_part
< 1K Download


Sun, 11 Dec 2005 15:17:02 GMT  
 
 [ 16 post ]  Go to page: [1] [2]

 Relevant Pages 

1. end-of-file during read using unformatted reading

2. read at end ... not at end, etc

3. Detecting end-of-line when reading a FILE?

4. Attempt to read past end of file

5. Bug in gfortran when reading end of file?

6. backspace error (reading file from the end)

7. how to deal with end of file when reading

8. Cals Begin and End Balance

9. prob. with BEGIN/END

10. Smalltalk on the small end (was: Advice requested: GUI project beginning)

11. How to write filesearch with begin-pattern till end-pattern

12. begin/end doesn't behave as expected

 

 
Powered by phpBB® Forum Software