Rob Dixon wrote:
"Alan C." <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...

Rob Dixon wrote:

   my $last = undef;

   while (<>)
   {
       next unless defined $last;
       chomp $last unless /^\./;
       print $last;
   } continue {
       $last = $_;
   }

   print $last;

<snipped>
Just to take yours apart, my trying to understand it, I thought next
would work.

while (<>) {
    chomp unless m/^\./;
    print "$_";
  }

But I tried it.  It does not work, and I don't know why.

Because you're removing the newline from the end of every line except those
starting with a period. You need to except the lines _before_ ones starting
with a period.
But (I thought that's what the unless does) I don't understand why the unless is in there?

chomp $last unless /^\./;

That reads to me: "chomp the string unless the string begins with a dot". My shorter version (that didn't work) is the same as yours except it uses Perl's default variables and is without the undef at beginning also without continue block.
Looking at it this way I wrote some code which used a one-line buffer, but
it had some ugly end conditions. Thinking further I much prefer this:

    while (<>)
    {
        chomp;
        s/^\./\n./;
        print;
    }
    print "\n";

which removes /all/ newlines, and then replaces them before initial periods.

Clever, simple, and easy. Though a bit new, I'm advanced enough that I could have thought of that one myself. But I didn't. Cheers to broadening my horizons!

<snipped>
'continue' isn't often used, but its absence is one of the failings of C
<snipped>
'defined $last' is one of my ugly end conditions. I explicitly declare 'my
$last = undef' (which is actually the same as 'my $last') so that the loop
can know that it is in its first execution and simply skip to the 'continue'
block (and there define $last).
I had referenced my books sufficiently to discover that (as long as it's undef) it skip (some code line(s) in its travel directly to the continue block (Perl In A Nutshell book)

But, as far as a scalar, a string, I'm not yet settled on what constitutes an undefined string. Help?

I tested the undef and if a string has a \n then it turns up as defined, meaning a blank line as a string is defined.

Thanks!

--
Alan.


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

Reply via email to