"Alan C." <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> 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".

No: "chomp $last unless $_ 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.

I use the default variable $_ as well as $last. In my code, $last holds the
the _previous_ file line, while $_ holds the current one. That's what I mean
about end conditions: you can't do anything until you've read line 2,
because before that there's no previous line. Also at the end you have to
spit out the last line, as the loop itself only ever outputs 'previous'
lines, and so will stop at the last line but one.

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

I should've thought of it first time round!

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

In general a string is undefined if nothing has yet been assigned to it.
It's not a real value in that you can compare against it ( $x == undef
doesn't work properly ) but you can test for it (if undef $x) and set a
variable back to being undefined (undef $x or $x = undef). An empty string,
whitespace, numeric zero etc. are all 'defined'.

HTH,

Rob




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

Reply via email to