Re: script help - stripping trailing spaces in exisitng script



Greg wrote:
Can some give me a hand to simplify a perl script? I currently run the
below script on the below sample data. The only problem is the trailing
spaces at the end of each line. A quick web search put me onto
"s/\s+$//" which I was easily able to add into another script that runs
on the data after the first one is finished with it and cleans up these
spaces perfectly.

This just seems very kludgy, can someone tell me how to change the
original to just include all this in a single script.

<snip>

printf(FILE2"%-9s%-8s%-80s\n",$col[2],$col[0],$col1[$i]);
------------------------^^^
Exchange that %-80s for %s. Same thing for next printf() statement.

The whole script can probably be simplified to something like:

  use strict;
  use warnings;
  my $file1 = "$ENV{TEMP}/65.txt";
  my $file2 = "$ENV{TEMP}/65_2.txt";
  open my $F1, $file1 or die "Can't open $file1: $!";
  open my $F2, "> $file2" or die "Can't create $file2: $!";
  while (<$F1>) {
    chomp;
    my @col = split /@/;
    if ( $col[1] and $col[1] ne '^(DS)' ) {
      my @col1 = split /_/, $col[1];
      printf $F2 "%-9s%-8s%s\n", ($col[2] or ''), $col[0], $col1[0];
      printf $F2 "%-9s%-8s%s\n", '', '', $col1[$_] for 1..$#col1;
    }
  }

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
.



Relevant Pages

  • Re: Grep and mv
    ... Cat -v shows no problems. ... My silly little grep script extracts the names as ... Since line 8 in your script cuts trailing spaces ...
    (comp.unix.shell)
  • Re: Looping/Counting
    ... second script bins the information for each 10 percent of the data. ... The last decile is further broken down into ... It turns out that for many deposits, 1% of the sample data ...
    (comp.lang.awk)
  • Re: Windows 2008 Scheduled Tasks
    ... command was on the same line as the Copy command, ... I then went back into my original script and I saw that the second Copy ... Apparently 2008 is less tolerant of these trailing spaces ... I've had numerous problems with scheduled tasks no longer running. ...
    (microsoft.public.windows.server.general)
  • Re: Need help
    ... Hi Mjuricek, ... enough information to know what the script does. ... Include all constraints but exclude irrelevant ... based on the sample data you gave. ...
    (microsoft.public.sqlserver.programming)
  • Help: Odd Output
    ... I wrote a script to check the location. ... Here's my codes. ... if ($num <= 2) ... Here's the sample data. ...
    (comp.lang.perl.misc)