CSV header getting lost along the way

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • erbrose
    New Member
    • Oct 2006
    • 58

    CSV header getting lost along the way

    Hey all, Im back and in need of some help again. Once again I have been tasked with fixing a problem in someone else script and am completely stuck. The process of the script works just fine (basically filling gaps in one csv file $ARGV[0] based on another $ARGV[2]) but in the output .csv the header is not there and there is an extra space added...

    [CODE=perl]
    #!/usr/bin/perl

    use File::Basename;

    #use strict;
    my $fileInput = $ARGV[0];
    my $fileOutput = $ARGV[1];
    my $fileTTable = $ARGV[2];
    my $fileO = $ARGV[0];

    my ( $filename, $finBase, $finDir ) = fileparse($file Output);
    my $fileOut = $finDir . $finBase . $filename;

    open( READER, $fileO ) or die "Can't open $file0: $!";
    open( TT, $fileTTable ) or die "Can't open $file0: $!";
    open( READ, $fileInput ) or die "Can't open $file0: $!";
    open( WRITE, ">$fileOut" ) or die "Can't open $file0: $!";

    my $maxDiff = 10;
    my %tmcHash = {};
    print WRITE "$category\ n";

    while (<READ>) {

    @line = split( /,/, $line1 );

    $line1 = $_;
    chomp($line1);
    }
    $hashValue = $line1;
    close(READ);
    $length = @line;
    my %ttHash = {};
    $header = $line[0];
    $tmcHash{"$head er"} = $hashValue;
    while (<TT>) {

    $ttLine = $_;
    chomp($ttLine);
    }
    @parts = split( /,/, $ttLine );
    close(TT);
    $key = $parts[0];
    $value = $parts[1] . "," . $parts[2];

    while (<READER>) $ttHash{"$key"} = $value;
    {

    $lin = $_;
    chomp($lin);
    @kkk = split( /,/, $lin );
    $k = $kkk[0];
    $temp1 = $tmcHash{"$k"};
    @tarTmc = split( /,/, $temp1 );

    @tarTmc2 = split( /,/, $temp1 );

    $temp2 = $ttHash{"$tarTm c[0]"};
    @adj = split( /,/, $temp2 );

    $spike = 0;
    my @preTmc = ();
    my @nextTmc = ();
    if ( $tmcHash{"$adj[0]"} ne "" ) {
    $temp3 = $tmcHash{"$adj[0]"};
    @preTmc = split( /,/, $temp3 );

    }
    if ( $tmcHash{"$adj[1]"} ne "" ) {
    $temp4 = $tmcHash{"$adj[1]"};
    @nextTmc = split( /,/, $temp4 );
    }
    $length = @tarTmc;
    for ( $j = 1 ; $j < $length - 1 ; $j++ ) {
    $count = 0;
    my @vs = ();

    if ( $tarTmc[$j] == -1 ) {
    $total = 0;

    if ( $tmcHash{"$adj[0]"} ne "" ) {
    if ( $preTmc[$j] != -1 ) {
    push( @vs, $preTmc[$j] );
    $count++;
    }
    }

    if ( $tmcHash{"$adj[1]"} ne "" ) {
    if ( $nextTmc[$j] != -1 ) {
    push( @vs, $nextTmc[$j] );
    $count++;
    }
    }

    $size = @vs;
    for ( $i = 0 ; $i < $size ; $i++ ) {
    $total += $vs[$i];
    }

    if ( $count != 0 ) {
    $tarTmc2[$j] = $total / $count;
    $tarTmc2[$j] = 10 * $tarTmc2[$j];
    $tarTmc2[$j] = round( $tarTmc2[$j] );
    $tarTmc2[$j] = $tarTmc2[$j] / 10;
    }

    }

    }
    print WRITE "$tarTmc[0]";

    }
    for ( $k = 1 ; $k < $length ; $k++ ) {
    print WRITE ",$tarTmc2[$k]";
    close(READER);
    }
    close(WRITE);
    print WRITE "\n";

    sub round {
    my ($number) = shift;
    return int($number);
    }
    [/CODE]


    I know too the use strict is commented out and thats a bad bad thing..
    I am assuming the issue has to do with $tarTMC[0] on line 128, but that is an assumption.. Anyways, I would greatly appreciate any thoughts on where i should look/research to help solve this problem
    Thanks ahead of time.
    Eric
    Last edited by miller; Aug 2 '07, 04:39 PM. Reason: Code Tag and Perltidy
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Greetings Eric,

    I'm not going to bother reading any code that doesn't have "use strict;" and "use warnings;" enabled and working without errors. However, when I ran perltidy on your code to clean it up, it reported an error at line 46 in the above formatted post version.

    - Miller

    Comment

    • erbrose
      New Member
      • Oct 2006
      • 58

      #3
      i appreciate your respone Miller...
      working to clean it up!
      Eric

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Yep, this looks wrong:


        while (<READER>) $ttHash{"$key"} = $value;

        {

        $ttHash{"$key"} = $value; is in the wrong place.

        Comment

        Working...