dudley has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I am very new to perl and have run into a weird problem. I have a comma seperated text file that I split each line into an array (based on the comma). I modify a few values in the array, and then print it to the screen and a text file. when I print it to the screen, the resulting lines look fine, but when I print it to a text file, each value in the list has a space added to the front of it. I could always post process this and fix it up after the fact, but I really want to understand what I am doing wrong. Please Help!
$filein = "sample.txt"; open(FILE, $filein) or die "Couldn't open file $file"; while (<FILE>) { $line = $_; (@values) = split ( /(,)/ , $line); foreach $value(@values) { if ($value =~ /-\d{2}/) {$value =~ s/-/0/} elsif ($value =~ /-\d{3}/) {$value =~ s/-//} } push (@newfile,@values); print @values; } $filein=~ s/.txt/.tst/; open (NEW, ">$filein"); print NEW "@newfile"; close (NEW);

Replies are listed 'Best First'.
Re: printing to a file
by matija (Priest) on Apr 02, 2004 at 18:44 UTC
    When you're printing to the screen, you use
    print @values;
    and when you print to the file, you use
    print NEW "@newfile";
    See the quotes? They make a difference. In the former case, it's print that expands the array, and in the later case, it is the string interpolation.

    Personally, I would say both methods are inadvisable: when you are printing arrays, determine their formating explicitly - for instance with a join.

      Or since it's CSV data use Text::CSV_XS and let it bother with quoting and what not.

      In addition...

      print uses $, as a "field separator" (the string which goes between elements of the argument list). String interpolation of arrays uses $" for the same purpose.

      The following one-liner prints the default value of these variables:

      $ perl -e 'print qq{\$,="$," \$"="$""\n}' $,="" $"=" "

      but when I print it to a text file, each value in the list has a space added to the front of it.

      Actually, the spaces are in between list elements

Re: printing to a file
by Plankton (Vicar) on Apr 02, 2004 at 18:45 UTC
    Why print to a file and to STDOUT (the screen). Why not just print to STDOUT and run your script like so ...
    $ myscript.pl < inout > output
    If you want to see the output on the screen do this ...
    $ myscript.pl < input | tee output

    Plankton: 1% Evil, 99% Hot Gas.
Re: printing to a file
by DigitalKitty (Parson) on Apr 02, 2004 at 19:09 UTC
    Hi dudley.

    I felt compelled to offer a little advice. In your open / close functions, I would include some minor modifications in the event of failure (hardware related, etc.).
    open(FILE, $filein) or die "Couldn't open file $filein : $!\n"; close (FILE) or die "Couldn't close file : $!\n"; open (NEW, ">$filein") or die "Couldn't open $filein : $!\n"; close (NEW) or die "Couldn't close file: $!\n";

    Even though perl will close filehandles for you, I feel it is unwise to not explicitly check for success / failure.

    Hope this helps,
    -Katie
Re: printing to a file
by tinita (Parson) on Apr 03, 2004 at 16:15 UTC
    there's also an entry in the great collection of frequently asked questions, perlfaq. searching for "weird spaces" would have found it.
Re: printing to a file
by dudley (Initiate) on Apr 02, 2004 at 19:05 UTC
    Thanks to everyone for the quick and informative replies! Porblem solved! The Perl Monks Rock!