"John W. Krahn" wrote:
>
> Stuart Clemons wrote:
> >
> > I'm trying to cleanup and format this text file of user names, so that I
> > have one column of user names. Here's the text file:
> >
> > The request will be processed at a domain controller for domain FOOBAR.
> >
> > Group name Misc
> > Comment These are the FOOBAR users
> >
> > Members
> >
> > -------------------------------------------------------------------------------
> > arod cschilling csenior
> > ecostello ffrank gbennett
> > LBird PMartinez
> > The command completed successfully.
>
> open FH, 'c:/foobar.txt' or die "sourcefile open failed - $!";
> while ( <FH> ) {
> next if 1 .. /^-+$/;
> last if /^The command completed successfully/;
> print "$_\n" for /\S+/g;
> }
Someone (off-list) asked for an explanation of this code.
while ( <FH> ) {
This reads the current line from the filehandle FH into the $_ variable
and sets the $. variable to the current line number.
next if 1 .. /^-+$/;
Using a constant integer with the range operator in a while loop is a
shortcut that compares $. to the constant so the range is everything
from the first line to the line that contains only hyphens. next sends
execution back to the beginning of the loop.
last if /^The command completed successfully/;
last exits the loop if the current line starts with the text 'The
command completed successfully'.
print "$_\n" for /\S+/g;
The regex in list context creates a list of all the non-whitespace
strings from the current line and each member of the list is printed out
with a trailing newline. You could also create the list with the split
function:
print "$_\n" for split;
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>