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!) -
#!/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
1 1857
Solved albeit backwards... and with the help of a textpad macro to tidy at the end... -
#!/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 :)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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. ...
|
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...
|
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 =...
|
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...
|
by: Sri |
last post by:
How do you add an n-bit number in C?
Regards,
Sri
|
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...
|
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 =...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |