"Akens, Anthony" wrote:

> > The files are all pipe-delimited, so I don't have a problem separating the
> > fields, I just am not sure how to make it remove all extra whitespace.  It
> > needs to keep all Space in the fields "        the    description   of   the
>
> >  file        " should still be readable as "the description of the file"
> >
> > Any help with code examples?  I have been looking through a beginning book
> > and my old code and have come up nil.

[Original post repositioned for readability]

> I figured I'd take a stab at fleshing this out into what he wants...
> Any comments on things I could do better?  I only added to what
> Robert had coded...
>
> Tony
>
> #!/usr/bin/perl -w
>
> use strict;
> my $dirname = "/my/stuff/";
> my $file;
> my $newfile;
> my $line;
>
> opendir (DIR, $dirname) or die "Can't opendir $dirname: $!";
> while (defined($file = readdir(DIR))) {
>         next if $file =~ /^\.\.?$/;
>         open (OLDFILE, "< $file");
>         $newfile = $file . "_nice";
>         open (NEWFILE, "> $newfile");
>         while ($line = <OLDFILE>)  {
>                 ($line) = ($line =~ /^\s*(.*)\s*\n$/);
>                 print NEWFILE "$line\n";
>         }
>         close OLDFILE;
>         close NEWFILE;
> }

Way too complicated.  The specifications are much more simple:  "Remove
extraneous whitespace".  This does not reuire any capturing parentheses.  Deal
only with whitespace:

while (<IN>) {
  chomp;
  s/^\s*//;    #trim start
  s/\s*$/;     #trim end
  s/\s(2,)/ /g;   # eliminate extraneous spce in between
  print OUT "$_\n";
}

More lines here, but a helluva lot less processing.

Joseph


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

Reply via email to