"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; > > > Your code works super for the job! > > I had thought of chomp and unless. But I'm too new, didn't know how to > implement them. > > 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. 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. > > perldoc -q continue turned up nothing. > No, it wouldn't (necessarily). 'perldoc -q' prints only answers to FAQs with your topic in the heading. 'perldoc -f' is said to print details of the given function, but will in fact document all language words as well. So: perldoc -f continue > > I understand the regex part of your code--looks like that line of code > means "chomp the line if it is a line that does not begin with a period" > > The first iteration, $last is not defined therefore next will make it > what (try on the next line of my text that's my guess) > You have 'redo', 'next', and 'last'. These take you to the following places in a loop: while (condition) REDO > { : NEXT > } continue { : } LAST > So 'redo' re-executes the main block from the beginning (without checking the while condition); 'next' goes to the end of the main block, and executes the continue block (if any) followed by the while condition; and 'last' breaks out of the loop immediately. 'continue' isn't often used, but its absence is one of the failings of C (which has a 'continue' statement but it is like Perl's 'next'). C's continue block is the 'loop expression' of the for() statement, and you can do some very convoluted stuff here with the comma operator. > (therefore when $last is undef it doesn't chomp but instead trys/begins > on the next line of my text????) > > providing condition met, "next" makes it skip to the next iteration? > > I don't understand the workings of 1. how it might potentially go from > undef to defined 2. what the continue does or how it works '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). > Meanwhile I'll be continuing my studies/practice with "Learning Perl > 3rd" and later I'll reference "Programming Perl 3rd" on continue and undef Sounds good to me. That'll make Randal (the Llama man) very happy :) Cheers Alan, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]