problem adding column to array

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

    #1

    problem adding column to array

    Oh perl masters, please show me the way!!! Alright, my issue in a nut shell is... I have a folder packed full of csv files. In each csv file there are 2 fields (NAME, VALUE). I need to combine all these files into one file BUT here is where I am stuck.. each csv is named by a specific data/24time (ie 20080220013000. csv).
    As i loop through these csv's I need to add the file name as a column, so I would end up with 3 fields in the combined new csv file (NAME,VALUE,DAT ETIME) this is so i can differentiate between the files when I go to do some final analysis.
    Right now I am just trying to add the DATETIME to one csv (will work on looping through all the files after I get this working) here is what i've managed to come up with so far (ps it does't work either!)

    Code:
    #!/usr/local/bin/perl
    #use strict;
    use warnings;
    use tie::File;
    my $InDir="D:\\input";
    my $OutDir="D:\\output";
    my $file_count=0;
    my $directory_count=0;
    my $file_out="temp_out.csv";
    
      opendir(DIR, $InDir);
        LINE: while(my $FILE = readdir(DIR)) {
          next LINE if($FILE =~ /^\.\.?/);
          ## check to see if it is a directory
          if(-d "$FILE"){
            $directory_count++;
            }
          else {
            $file_count++;
          }
        }
      closedir(DIR);
      
    my @dirSen;  
      opendir(DIR, $InDir) or die "Unable to open dir $InDir: $!\n";
      @dirSen = sort readdir(DIR);
      my $dir_sen_contents = $dirSen[2];
      close(DIR);
      #$dir_sen_contents is now the first file of sensor directory
      #print "$InDir\\$dir_sen_contents\n";
    
    open (FILE, "$InDir\\$dir_sen_contents") || die "Cannot write to $dir_sen_contents\n"; 
    open (TMP, ">$OutDir\\$file_out") || die "Cannot write to $file_out\n"; 
    my @TMP=(<FILE>);
    my $tmp2=@TMP;
    for(my $i = 0; $i < $tmp2; $i +=1) {
      $TMP[$i][3] = "$dir_sen_contents"}
    print @TMP;
     close FILE;
     close TMP;
    ps. i get an error Can't use string ("NAME,VALUE ") as an array ref while "stricts ref" in use at line 37
    Any assistance will be greatly appreciated.
    Thanks ahead of time
    Eric
  • erbrose
    New Member
    • Oct 2006
    • 58

    #2
    Solved albeit backwards... and with the help of a textpad macro to tidy at the end...
    Code:
    #!/usr/local/bin/perl
    use strict;
    use warnings;
    use tie::File;
    use File::Copy;
    
    
    my $pid;
    die "cant fork $!\n" unless defined($pid=fork());
    
     if($pid) {
     print "Here we go\n";
     my $in='go';
       while ($in ne 'stop') {
       print "$in is your input\n";
           if($in eq 'stop') {
           print "exiting.........\n";
           }
       }
     }
     else {
    LOOP1:
      sleep 1;
      {move_files()}
    
    goto LOOP1;
      
     }
     
    
    sub move_files { 
    
    my $InDir="D:\\input";
    my $TmpDir="D:\\temp";
    my $OutDir="D:\\output";
    my $file_count=0;
    my $directory_count=0;
    my $file_out="combined_data.csv";
    
      opendir(DIR, $InDir);
        LINE: while(my $FILE = readdir(DIR)) {
          next LINE if($FILE =~ /^\.\.?/);
          ## check to see if it is a directory
          if(-d "$FILE"){
            $directory_count++;
            }
          else {
            $file_count++;
          }
        }
      closedir(DIR);
      
    my @dirSen;  
      opendir(DIR, $InDir) or die "Unable to open dir $InDir: $!\n";
      @dirSen = sort readdir(DIR);
      my $dir_sen_contents = $dirSen[2];
      close(DIR);
      #$dir_sen_contents is now the first file of sensor directory
      
    my $tmpyear=substr $dir_sen_contents,6,4;
    my $tmpmonth=substr $dir_sen_contents,10,2;
    my $tmpday=substr $dir_sen_contents,12,2;
    my $tmphour=substr $dir_sen_contents,14,2;
    my $tmpminute=substr $dir_sen_contents,16,2;
    my $tmpsecond=substr $dir_sen_contents,18,2;
    my $field=$tmpyear."-".$tmpmonth."-".$tmpday." ".$tmphour.":".$tmpminute.":".$tmpsecond;
    my $movefrom="$InDir\\$dir_sen_contents";
    my $moveto="$TmpDir\\$dir_sen_contents";
    print $movefrom;
    print $moveto;
    
    open (FILE, "$InDir\\$dir_sen_contents") || die "Cannot write to $dir_sen_contents\n"; 
    open (TMP, ">>$OutDir\\$file_out") || die "Cannot write to $dir_sen_contents\n"; 
      while (<FILE>) {
        print TMP ("$field",",$_");
        }
      
    close FILE;
    close TMP;
    move ("$movefrom", "$moveto") or die "failed to move"; 
    }
    would still definitely love to see someones code that isn't as cluttered as mine if they have the time :)

    Comment

    Working...