---------- Forwarded message ----------
Date: Fri, 19 Apr 2002 19:06:30 -0400 (EDT)
From: Jeff 'japhy' Pinyan <[EMAIL PROTECTED]>
Reply-To: [EMAIL PROTECTED]
To: drieux <[EMAIL PROTECTED]>
Subject: Re: B/C - Re: help parsing file

On Apr 19, drieux said:

>>   while (<FH>) {
>>     next unless /^$prefix/;
>>     $_ .= <FH> while /^$prefix/gm;
>>     print;
>>   }
>
>why is it that when I try to do this - my perldebugger
>goes out to lunch...

Because I messed up.  I never actually ran that code -- it was what I
thought of doing, and it mutated to the final working code.  Sorry.

The //g trick only works when the variable isn't changed.  However, I'm
appending to $_, so the trick breaks.  The trick can be unbroken in the
following manner:

  while (<FH>) {
    next unless /^$prefix/;
    pos = (pos, $_ .= <FH>)[0] while /^$prefix/gm;
    print;
  }

pos() happens to hold the place where the next //g match starts from in
the string it matches against.  It can be read from or written to.

  $p = pos $str;
  pos $str = 5;   # that's pos($str) = 5, not pos($str = 5)

pos()'s default argument is $_.  So we restore pos() to the value it had
prior to changing $_, and change $_ all at once.  Trickery.

This cruft is another reason to use the flip-flop approach.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to