Getting rid of white space...

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default RE: Getting rid of white space...

    are you speaking of this?

    use strict;
    while(my $line = <>) {
    ($line) = ($line =~ /^\s*(.*)\s*\n$/);
    print($line."_nice/n");
    }

    -----Original Message-----
    From: LoneWolf [mailto:lonewolf@nc.rr.com]
    Sent: Thursday, September 04, 2003 3:38 PM
    To: [email]beginners@perl.org[/email]
    Subject: Getting rid of white space...


    I have about 12 files that I am pulling for a SCO box to a RedHat box, FTP.
    THe files from the SCO box are poorly formatted with extraneous whitespace
    (sometimes as much as 30 or more) before and after the text. I need to
    parse all of the files I DL and put them into a new file with "_nice" added
    at the end.

    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.

    Thanks,
    Robert

    --
    To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    Marcos Rebelo Guest

  2. Similar Questions and Discussions

    1. Logo on Web - White Space
      Using a corporate logo on my web page. It's a TIF. I can change the color of the logo graphic (round, but on the web page the logo displays on a...
    2. how to contract white space
      I started using Director 7 on PC and I am unable to contract white space around my cast members. The casts members are are drawn by pencil and are...
    3. Trim White Space
      I imported a bitmap with white spaces at the borders whithout trimming it. That's right, it's gone fine, i have my image with the white spaces. But...
    4. White space around the whole page
      How can I get rid of the white space around my page, when I preview it in IE6?
    5. White Space around pop-up window
      Hi All I have created a bunch of thumbnail images that when clicked on would open up a new window. I have sized the window pop-up to be the exact...
  3. #2

    Default RE: Getting rid of white space...

    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;
    }


    -----Original Message-----
    From: [email]Marcos.Rebelo@eurocopter.com[/email] [mailto:Marcos.Rebelo@eurocopter.com]
    Sent: Thursday, September 04, 2003 8:32 AM
    To: [email]LoneWolf@nc.rr.com[/email]; [email]beginners@perl.org[/email]
    Subject: RE: Getting rid of white space...


    are you speaking of this?

    use strict;
    while(my $line = <>) {
    ($line) = ($line =~ /^\s*(.*)\s*\n$/);
    print($line."_nice/n");
    }

    -----Original Message-----
    From: LoneWolf [mailto:lonewolf@nc.rr.com]
    Sent: Thursday, September 04, 2003 3:38 PM
    To: [email]beginners@perl.org[/email]
    Subject: Getting rid of white space...


    I have about 12 files that I am pulling for a SCO box to a RedHat box, FTP.
    THe files from the SCO box are poorly formatted with extraneous whitespace
    (sometimes as much as 30 or more) before and after the text. I need to
    parse all of the files I DL and put them into a new file with "_nice" added
    at the end.

    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.

    Thanks,
    Robert

    --
    To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    For additional commands, e-mail: [email]beginners-help@perl.org[/email]

    --
    To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    For additional commands, e-mail: [email]beginners-help@perl.org[/email]

    Anthony Akens Guest

  4. #3

    Default Re: Getting rid of white space...

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    On Thu, 04 Sep 2003 06:37:57 -0700, LoneWolf wrote:
    > I have about 12 files that I am pulling for a SCO box to a RedHat box, FTP.
    > THe files from the SCO box are poorly formatted with extraneous whitespace
    > (sometimes as much as 30 or more) before and after the text. I need to
    > parse all of the files I DL and put them into a new file with "_nice" added
    > at the end.
    >
    > 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.
    - From your example it appears that you want to remove all whitespace from
    the beginning, and end, and duplicates from inside. You can achieve this
    easily:

    my $text = ' the description of the file ';
    for($text) {
    s/^\s+//;
    s/\s+$//;
    # Perhaps its more effecient to put this next line first?
    s/\s+/ /g;
    }

    Also, check "perldoc -q space".

    - Brian
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.2 (GNU/Linux)

    iD8DBQE/V1ohiK/rA3tCpFYRAsGpAJ0XWKLjN9+7MYBnfeFz1g/DBWsqtgCg80v1
    COz0Otn5XsqmO3D/ErXKkjQ=
    =EgAZ
    -----END PGP SIGNATURE-----


    Brian Harnish Guest

  5. #4

    Default RE: Getting rid of white space...

    I have a similar problem to this , does anyone have another answer? Don't know if its just me but The
    > use strict;
    > while(my $line = <>) {
    > ($line) = ($line =~ /^\s*(.*)\s*\n$/);
    > print($line."_nice/n");
    > }
    Code doesn't work , and the enhanced version with the fancy file handling doesn't work either "trying to read from a closed filehandle error"

    Stephen
    > -----Original Message-----
    > From: Akens, Anthony [mailto:AAkens@egh.org]
    > Sent: 04 September 2003 15:55
    > To: [email]Marcos.Rebelo@eurocopter.com[/email]; [email]LoneWolf@nc.rr.com[/email];
    > [email]beginners@perl.org[/email]
    > Subject: RE: Getting rid of white space...
    >
    >
    > 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;
    > }
    >
    >
    > -----Original Message-----
    > From: [email]Marcos.Rebelo@eurocopter.com[/email]
    > [mailto:Marcos.Rebelo@eurocopter.com]
    > Sent: Thursday, September 04, 2003 8:32 AM
    > To: [email]LoneWolf@nc.rr.com[/email]; [email]beginners@perl.org[/email]
    > Subject: RE: Getting rid of white space...
    >
    >
    > are you speaking of this?
    >
    > use strict;
    > while(my $line = <>) {
    > ($line) = ($line =~ /^\s*(.*)\s*\n$/);
    > print($line."_nice/n");
    > }
    >
    > -----Original Message-----
    > From: LoneWolf [mailto:lonewolf@nc.rr.com]
    > Sent: Thursday, September 04, 2003 3:38 PM
    > To: [email]beginners@perl.org[/email]
    > Subject: Getting rid of white space...
    >
    >
    > I have about 12 files that I am pulling for a SCO box to a
    > RedHat box, FTP.
    > THe files from the SCO box are poorly formatted with
    > extraneous whitespace (sometimes as much as 30 or more)
    > before and after the text. I need to parse all of the files
    > I DL and put them into a new file with "_nice" added at the end.
    >
    > 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.
    >
    > Thanks,
    > Robert
    >
    > --
    > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    >
    > --
    > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    >
    >
    > --
    > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    >
    Stephen Marshall Guest

  6. #5

    Default RE: Getting rid of white space...

    Got it working this way fror the important line, but theres probably a slicker way of doing it.

    $line =~ s/(\s)+/ /g;
    > -----Original Message-----
    > From: Marshall, Stephen
    > Sent: 04 September 2003 17:07
    > To: 'Akens, Anthony'; [email]Marcos.Rebelo@eurocopter.com[/email];
    > [email]LoneWolf@nc.rr.com[/email]; [email]beginners@perl.org[/email]
    > Subject: RE: Getting rid of white space...
    >
    >
    > I have a similar problem to this , does anyone have another
    > answer? Don't know if its just me but The
    >
    > > use strict;
    > > while(my $line = <>) {
    > > ($line) = ($line =~ /^\s*(.*)\s*\n$/);
    > > print($line."_nice/n");
    > > }
    >
    > Code doesn't work , and the enhanced version with the fancy
    > file handling doesn't work either "trying to read from a
    > closed filehandle error"
    >
    > Stephen
    >
    > > -----Original Message-----
    > > From: Akens, Anthony [mailto:AAkens@egh.org]
    > > Sent: 04 September 2003 15:55
    > > To: [email]Marcos.Rebelo@eurocopter.com[/email]; [email]LoneWolf@nc.rr.com[/email];
    > > [email]beginners@perl.org[/email]
    > > Subject: RE: Getting rid of white space...
    > >
    > >
    > > 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;
    > > }
    > >
    > >
    > > -----Original Message-----
    > > From: [email]Marcos.Rebelo@eurocopter.com[/email]
    > > [mailto:Marcos.Rebelo@eurocopter.com]
    > > Sent: Thursday, September 04, 2003 8:32 AM
    > > To: [email]LoneWolf@nc.rr.com[/email]; [email]beginners@perl.org[/email]
    > > Subject: RE: Getting rid of white space...
    > >
    > >
    > > are you speaking of this?
    > >
    > > use strict;
    > > while(my $line = <>) {
    > > ($line) = ($line =~ /^\s*(.*)\s*\n$/);
    > > print($line."_nice/n");
    > > }
    > >
    > > -----Original Message-----
    > > From: LoneWolf [mailto:lonewolf@nc.rr.com]
    > > Sent: Thursday, September 04, 2003 3:38 PM
    > > To: [email]beginners@perl.org[/email]
    > > Subject: Getting rid of white space...
    > >
    > >
    > > I have about 12 files that I am pulling for a SCO box to a
    > > RedHat box, FTP.
    > > THe files from the SCO box are poorly formatted with
    > > extraneous whitespace (sometimes as much as 30 or more)
    > > before and after the text. I need to parse all of the files
    > > I DL and put them into a new file with "_nice" added at the end.
    > >
    > > 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.
    > >
    > > Thanks,
    > > Robert
    > >
    > > --
    > > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    > >
    > > --
    > > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    > >
    > >
    > > --
    > > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    > >
    >
    Stephen Marshall Guest

  7. #6

    Default RE: Getting rid of white space...

    LoneWolf wrote:
    > I have about 12 files that I am pulling for a SCO box to a RedHat
    > box, FTP. THe files from the SCO box are poorly formatted with
    > extraneous whitespace (sometimes as much as 30 or more) before and
    > after the text. I need to parse all of the files I DL and put them
    > into a new file with "_nice" added at the end.
    >
    > 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"
    It's a little unclear what you're asking for. You say you want to keep all
    space in the fields, but your example compresses multiple spaces between
    words into single spaces.

    Anyway, assuming the following:

    1. You have split the fields into an array, say @fields, and
    2. You want to remove all leading and trailing whitespace, and
    3. You want to compress multiple internal whitespace chars into a single
    space

    I would do:

    s/^\s+//, s/\s+$//, s/\s\s+/ /g for @fields;

    The first two regexes are the canonical way to remove leading and trailing
    whitespace. The third compresses the internal whitespace.
    >
    > Any help with code examples? I have been looking through a beginning
    > book and my old code and have come up nil.
    Bob Showalter Guest

  8. #7

    Default RE: Getting rid of white space...

    Try it like this... I was a slacker and didn't have warnings
    in place if it couldn't open the file... Should be able
    to just change the "/my/stuff/" to your directory and have it
    work, I tested it here, and it seems to go fine. (on an aix
    box - maybe I have something in place that doesn't work on
    windows or other platforms?)

    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") or die "Can't open $file: $!";
    $newfile = $file . "_nice";
    open (NEWFILE, "> $newfile") or die "Can't open $newfile: $!";
    while ($line = <OLDFILE>) {
    ($line) = ($line =~ /^\s*(.*)\s*\n$/);
    print NEWFILE "$line\n";
    }
    close OLDFILE;
    close NEWFILE;
    }

    -----Original Message-----
    From: Marshall, Stephen [mailto:stephen.marshall@thus.net]
    Sent: Thursday, September 04, 2003 11:11 AM
    To: Marshall, Stephen; Akens, Anthony; 'Marcos.Rebelo@eurocopter.com';
    'LoneWolf@nc.rr.com'; 'beginners@perl.org'
    Subject: RE: Getting rid of white space...


    Got it working this way fror the important line, but theres probably a slicker way of doing it.

    $line =~ s/(\s)+/ /g;
    > -----Original Message-----
    > From: Marshall, Stephen
    > Sent: 04 September 2003 17:07
    > To: 'Akens, Anthony'; [email]Marcos.Rebelo@eurocopter.com[/email];
    > [email]LoneWolf@nc.rr.com[/email]; [email]beginners@perl.org[/email]
    > Subject: RE: Getting rid of white space...
    >
    >
    > I have a similar problem to this , does anyone have another
    > answer? Don't know if its just me but The
    >
    > > use strict;
    > > while(my $line = <>) {
    > > ($line) = ($line =~ /^\s*(.*)\s*\n$/);
    > > print($line."_nice/n");
    > > }
    >
    > Code doesn't work , and the enhanced version with the fancy
    > file handling doesn't work either "trying to read from a
    > closed filehandle error"
    >
    > Stephen
    >
    > > -----Original Message-----
    > > From: Akens, Anthony [mailto:AAkens@egh.org]
    > > Sent: 04 September 2003 15:55
    > > To: [email]Marcos.Rebelo@eurocopter.com[/email]; [email]LoneWolf@nc.rr.com[/email];
    > > [email]beginners@perl.org[/email]
    > > Subject: RE: Getting rid of white space...
    > >
    > >
    > > 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;
    > > }
    > >
    > >
    > > -----Original Message-----
    > > From: [email]Marcos.Rebelo@eurocopter.com[/email]
    > > [mailto:Marcos.Rebelo@eurocopter.com]
    > > Sent: Thursday, September 04, 2003 8:32 AM
    > > To: [email]LoneWolf@nc.rr.com[/email]; [email]beginners@perl.org[/email]
    > > Subject: RE: Getting rid of white space...
    > >
    > >
    > > are you speaking of this?
    > >
    > > use strict;
    > > while(my $line = <>) {
    > > ($line) = ($line =~ /^\s*(.*)\s*\n$/);
    > > print($line."_nice/n");
    > > }
    > >
    > > -----Original Message-----
    > > From: LoneWolf [mailto:lonewolf@nc.rr.com]
    > > Sent: Thursday, September 04, 2003 3:38 PM
    > > To: [email]beginners@perl.org[/email]
    > > Subject: Getting rid of white space...
    > >
    > >
    > > I have about 12 files that I am pulling for a SCO box to a
    > > RedHat box, FTP.
    > > THe files from the SCO box are poorly formatted with
    > > extraneous whitespace (sometimes as much as 30 or more)
    > > before and after the text. I need to parse all of the files
    > > I DL and put them into a new file with "_nice" added at the end.
    > >
    > > 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.
    > >
    > > Thanks,
    > > Robert
    > >
    > > --
    > > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    > >
    > > --
    > > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    > >
    > >
    > > --
    > > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    > >
    >
    Anthony Akens Guest

  9. #8

    Default RE: Getting rid of white space...

    On Thursday, September 04, 2003 11:11, Marshall, Stephen wrote:
    >
    >Got it working this way fror the important line, but theres probably a
    slicker way of doing it.
    >
    >$line =~ s/(\s)+/ /g;
    >
    This will work, but may leave an extraneous space at the beginning and/or
    end of the line.

    This text:

    " Test text with lots of extra spaces "

    would get changed to:

    " Test text with lots of extra spaces "

    which may not be what you want.

    If you want to eliminate any starting or ending space and trim the rest of
    it down to single spaces, I would suggest this:

    $line =~ s/\s+/ /g; # the parens you had are not necessary
    $line =~ s/^ //; # removes any space from the beginning of the line
    $line =~ s/ $//; # removes any space from the end of the line

    You could probably get fancier on the statements, but I prefer the
    simplicity of three separate statements.

    HTH,

    Alan
    Alan Perry Guest

  10. #9

    Default Re: Getting rid of white space...

    Lonewolf wrote:
    >
    > I have about 12 files that I am pulling for a SCO box to a RedHat box, FTP.
    > THe files from the SCO box are poorly formatted with extraneous whitespace
    > (sometimes as much as 30 or more) before and after the text. I need to
    > parse all of the files I DL and put them into a new file with "_nice" added
    > at the end.
    >
    > 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.
    #!/usr/bin/perl
    use warnings;
    use strict;

    ( $\, $^I ) = ( $/, '.bak' );

    while ( <> ) {
    s/^\s+//; # remove space at beginning of line
    s/\s+$//; # remove space at end of line
    s/\s*\|\s*/|/g; # remove space around pipe separator
    print;
    if ( eof ) {
    close ARGV;
    rename $ARGV, "${ARGV}_nice" or warn "Cannot rename $ARGV to ${ARGV}_nice: $!";
    }
    }

    __END__



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  11. #10

    Default Re: Getting rid of white space...

    "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

    R. Joseph Newton Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139