473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

problem adding column to array

58
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,DATETIME) 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!)

Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2. #use strict;
  3. use warnings;
  4. use tie::File;
  5. my $InDir="D:\\input";
  6. my $OutDir="D:\\output";
  7. my $file_count=0;
  8. my $directory_count=0;
  9. my $file_out="temp_out.csv";
  10.  
  11.   opendir(DIR, $InDir);
  12.     LINE: while(my $FILE = readdir(DIR)) {
  13.       next LINE if($FILE =~ /^\.\.?/);
  14.       ## check to see if it is a directory
  15.       if(-d "$FILE"){
  16.         $directory_count++;
  17.         }
  18.       else {
  19.         $file_count++;
  20.       }
  21.     }
  22.   closedir(DIR);
  23.  
  24. my @dirSen;  
  25.   opendir(DIR, $InDir) or die "Unable to open dir $InDir: $!\n";
  26.   @dirSen = sort readdir(DIR);
  27.   my $dir_sen_contents = $dirSen[2];
  28.   close(DIR);
  29.   #$dir_sen_contents is now the first file of sensor directory
  30.   #print "$InDir\\$dir_sen_contents\n";
  31.  
  32. open (FILE, "$InDir\\$dir_sen_contents") || die "Cannot write to $dir_sen_contents\n"; 
  33. open (TMP, ">$OutDir\\$file_out") || die "Cannot write to $file_out\n"; 
  34. my @TMP=(<FILE>);
  35. my $tmp2=@TMP;
  36. for(my $i = 0; $i < $tmp2; $i +=1) {
  37.   $TMP[$i][3] = "$dir_sen_contents"}
  38. print @TMP;
  39.  close FILE;
  40.  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
Jun 10 '08 #1
1 1857
erbrose
58
Solved albeit backwards... and with the help of a textpad macro to tidy at the end...
Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2. use strict;
  3. use warnings;
  4. use tie::File;
  5. use File::Copy;
  6.  
  7.  
  8. my $pid;
  9. die "cant fork $!\n" unless defined($pid=fork());
  10.  
  11.  if($pid) {
  12.  print "Here we go\n";
  13.  my $in='go';
  14.    while ($in ne 'stop') {
  15.    print "$in is your input\n";
  16.        if($in eq 'stop') {
  17.        print "exiting.........\n";
  18.        }
  19.    }
  20.  }
  21.  else {
  22. LOOP1:
  23.   sleep 1;
  24.   {move_files()}
  25.  
  26. goto LOOP1;
  27.  
  28.  }
  29.  
  30.  
  31. sub move_files { 
  32.  
  33. my $InDir="D:\\input";
  34. my $TmpDir="D:\\temp";
  35. my $OutDir="D:\\output";
  36. my $file_count=0;
  37. my $directory_count=0;
  38. my $file_out="combined_data.csv";
  39.  
  40.   opendir(DIR, $InDir);
  41.     LINE: while(my $FILE = readdir(DIR)) {
  42.       next LINE if($FILE =~ /^\.\.?/);
  43.       ## check to see if it is a directory
  44.       if(-d "$FILE"){
  45.         $directory_count++;
  46.         }
  47.       else {
  48.         $file_count++;
  49.       }
  50.     }
  51.   closedir(DIR);
  52.  
  53. my @dirSen;  
  54.   opendir(DIR, $InDir) or die "Unable to open dir $InDir: $!\n";
  55.   @dirSen = sort readdir(DIR);
  56.   my $dir_sen_contents = $dirSen[2];
  57.   close(DIR);
  58.   #$dir_sen_contents is now the first file of sensor directory
  59.  
  60. my $tmpyear=substr $dir_sen_contents,6,4;
  61. my $tmpmonth=substr $dir_sen_contents,10,2;
  62. my $tmpday=substr $dir_sen_contents,12,2;
  63. my $tmphour=substr $dir_sen_contents,14,2;
  64. my $tmpminute=substr $dir_sen_contents,16,2;
  65. my $tmpsecond=substr $dir_sen_contents,18,2;
  66. my $field=$tmpyear."-".$tmpmonth."-".$tmpday." ".$tmphour.":".$tmpminute.":".$tmpsecond;
  67. my $movefrom="$InDir\\$dir_sen_contents";
  68. my $moveto="$TmpDir\\$dir_sen_contents";
  69. print $movefrom;
  70. print $moveto;
  71.  
  72. open (FILE, "$InDir\\$dir_sen_contents") || die "Cannot write to $dir_sen_contents\n"; 
  73. open (TMP, ">>$OutDir\\$file_out") || die "Cannot write to $dir_sen_contents\n"; 
  74.   while (<FILE>) {
  75.     print TMP ("$field",",$_");
  76.     }
  77.  
  78. close FILE;
  79. close TMP;
  80. move ("$movefrom", "$moveto") or die "failed to move"; 
  81. }
would still definitely love to see someones code that isn't as cluttered as mine if they have the time :)
Jun 10 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Marcus | last post by:
I am having some problems with trying to perform calculations on time fields. Say I have a start time and an end time, 1:00:00 and 2:30:00 (on a 24 hour scale, not 12). I want to find the...
33
by: Geoff Berrow | last post by:
I may have mentioned that I run an Introduction to PHP course at a local college (very basic - I'm no PHP expert). Well, one of my students was doing really well so I set him some extension work. ...
1
by: Simon Harvey | last post by:
Hi, I'm hoping someone can help me witht he following problem: I have a fairly simple page that has a sort form and a button for adding the forms details to an arraylist. When the button is...
1
by: tangus via DotNetMonster.com | last post by:
Hello all, I'm really struggling with getting some Active Directory code to work in ASP.NET. Can you please provide assistance? I am executing the following code: Dim enTry As DirectoryEntry =...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
17
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
11
by: dennis.sprengers | last post by:
Consider the following multi-dimensional array: --------------------------- $arr = array( array(3, 5, 7, 9), array(2, 4, 6, 8), array(1, 3, 5, 7) ); function add_arrays($arr) { for ($row =...
60
by: Bill Cunningham | last post by:
I have a row of values like such, placed in a text file by fprintf. 10.50 10.25 10.00 10.75 11.00 What I want to do to the above colum is add a new column right beside it which is a total...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.