"Scott.Lutz" wrote:
> 
> This is getting a touch off base from where I wanted to go, but the jist
> of what I started with is this.
> Open a .csv file, enter every record into the array, one at a time, then
> copy the first value to the third last value, then print this value back
> to a new .csv file, including the comma separating the values.  I have
> all of these bases covered, except that it is not outputting to the new
> file with the commas intact. Below is the original snip, as this problem
> is still needing a final solution.
> 
> ___Snip___
> #!/usr/bin/perl

#!/usr/bin/perl -w
use strict;
# these two lines would have caught most of your mistakes

> open (FIN, "<statsfinal2.csv") || die "cant open INPUT file"; @data =
> <FIN>; close (FIN);
> 
> open (FOUT, "> STATS_output.csv") || die "cant open OUTPUT file";
> 
> my $output;
> 
> print "Starting !\n";
> $count = "1";
> foreach $line (@data) {
>         chomp;

Here you are chomp()ing $_ but your data is in $line

>       if ( $line eq "" ) { next; }

Because $line isn't chomp()ed this will never be true.

>         else {
>         my @string = split(/,/, $line);
>                 if (@string[0,1] eq "" ) { next; }

An array in a scalar context will return the number of elements in the array so this 
statement is the same as:

                 if ( 2 eq "" ) { next; }


>                 else {
>                 $copy_data = $string[0];
>                 $string[34] = $copy_data;
>                 #print join( ',', @output),"\n";
>                 #print (@output);
>                 @string =~ join( ',', @string),"\n";

=~ is used to bind scalars to m//, s///, or tr///.  It is a syntax error.

>                 push (@output, "@string");
>                 print "Line $count parsed with FIRST -> $string[0] and
> LAST -> $string[34]\n"; # visual aide
>                 }
>                 $count++;
>         }
> print FOUT @output;
> 
> close (FOUT);


Here is how I would do it:


#!/usr/bin/perl -w
use strict;

open FIN,  '< statsfinal2.csv'  or die "cant open statsfinal2.csv: $!";
open FOUT, '> STATS_output.csv' or die "cant open STATS_output.csv: $!";

print "Starting !\n";
while ( <FIN> ) {
    chomp;
    next unless /\S/;
    my @string = split /,/;
    next unless length $string[0] and length $string[1];
    @string[0,34] = @string[34,0];
    print FOUT join( ',', @string ), "\n";
    print "Line $. parsed with FIRST -> $string[0] and LAST -> $string[34]\n"; # 
visual aide
    }

close FOUT;
close FIN;

__END__



John
-- 
use Perl;
program
fulfillment

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

Reply via email to