Link to home
Start Free TrialLog in
Avatar of Begunix
Begunix

asked on

data manipulation perl

Sample data.csv

12/27/2006,73,1100,Wed
12/27/2006,73,1200,Wed
12/28/2006,99,1200,Thu
12/28/2006,99,1300,Thu
12/28/2006,99,1400,Thu
12/29/2006,52,1400,Fri
12/29/2006,52,1500,Fri
12/30/2006,46,1700,Sat
12/30/2006,45,1800,Sat
12/31/2006,46,1800,Sun
01/01/2007,51,1200,Mon
01/01/2007,51,1200,Mon
01/02/2007,54,1200,Tue
01/02/2007,54,1200,Tue


01/03/2007 will be Wed.12/27/2006 corresponds to Wed. So,01/03/2007 will have 73,1100 corresponding to 12/27/2006
and so on for 01/04/2007.

Output outputfile.csv should have data for next 2 days
01/03/2007,73,1100,Wed
01/03/2007,73,1200,Wed
01/04/2007,99,1200,Thu
01/04/2007,99,1300,Thu
01/04/2007,99,1400,Thu

Please advise the perl script.

Thanks in advance.
Begunix
Avatar of FishMonger
FishMonger
Flag of United States of America image

#!/usr/bin/perl

use warnings;
use strict;
use Date::Calc qw(Add_Delta_Days);

while (<DATA>) {
   my @line = split /,/;
   my ($mth, $day, $yr) = split /\//, $line[0];
   ($yr, $mth, $day) = Add_Delta_Days($yr, $mth, $day, 7);
   $line[0] = sprintf("%02d/%02d/%d", $mth, $day, $yr);
   print join ',', @line;
}



__DATA__
12/27/2006,73,1100,Wed
12/27/2006,73,1200,Wed
12/28/2006,99,1200,Thu
12/28/2006,99,1300,Thu
12/28/2006,99,1400,Thu
12/29/2006,52,1400,Fri
12/29/2006,52,1500,Fri
12/30/2006,46,1700,Sat
12/30/2006,45,1800,Sat
12/31/2006,46,1800,Sun
01/01/2007,51,1200,Mon
01/01/2007,51,1200,Mon
01/02/2007,54,1200,Tue
01/02/2007,54,1200,Tue
Avatar of Begunix
Begunix

ASKER

Thanks for the reply. But I do not have root access or admin rights. So, I installed Date::Calc module in /my/perldirectory and ran the following script.
#!/usr/bin/perl
use lib "/my/perldirectory";
use warnings;
use strict;

while (<DATA>) {
   my @line = split /,/;
   my ($mth, $day, $yr) = split /\//, $line[0];
   ($yr, $mth, $day) = Add_Delta_Days($yr, $mth, $day, 7);
   $line[0] = sprintf("%02d/%02d/%d", $mth, $day, $yr);
   print join ',', @line;
}

I got a compilation error. Cannot find subroutine Add_Delta_Days.
Please advise changes to the above script or script using only inbuilt modules.
Thanks
Begunix
You didn't load the module.

add this:

use Date::Calc qw(Add_Delta_Days);
Also, in my example the DATA is hard coded in the script.  Run your test by adding your use lib statement to the example script I posted and assuming you installed the module correctly, it should work.

If it doesn't give you the desired results, then please provide more details on what you need.
Avatar of Begunix

ASKER

I still got the same error.
The __DATA__ can have different dates. Please advise script by using only inbuilt modules.
12/27/2006,73,1100,Wed
12/27/2006,73,1200,Wed
12/28/2006,99,1200,Thu
12/28/2006,99,1300,Thu
12/28/2006,99,1400,Thu
12/29/2006,52,1400,Fri
12/29/2006,52,1500,Fri
12/30/2006,46,1700,Sat
12/30/2006,45,1800,Sat
12/31/2006,46,1800,Sun
01/01/2007,51,1200,Mon
01/01/2007,51,1200,Mon
01/02/2007,54,1200,Tue
01/02/2007,54,1200,Tue



Thanks
Begunix
Please post the entire script that you tested and the exact complete error message.

Exactly how did you install the Data::Calc module?

I'm not exactly sure I understand what you need.  The example script that I gave takes your sample data and adds 7 days to the date then prints out the new data.  Is that what you need?  If not please provide more details.
Avatar of Begunix

ASKER

downloaded gzip files in my directory from cpan or mit.edu
downloaded required module say Date-Calc-5.4.tar.gz
On a Unix machine, this can be done with the tar and gzip programs, which are almost certainly installed on your machine.
       1) gunzip Date-Calc-5.4.tar.gz
       2) tar -xf Date-Calc-5.4.tar
When I run the 2) step, I see data like

tar:Date-Calc-5.4/t/f023.t : Permission denied.
...
tar: Date-Calc-5.4/examples/income.pl : Permission denied.
...
tar: Date-Calc-5.4/Calc.pm : Permission denied.


This should create a subdirectory called (in this case) Date-Calc-5.4.
But incorrect directory Date-Calc-5.4 is created.

3) perl Makefile.PL PREFIX=/my/perldirectory
Note:

4) make install
(Remember that if you do this, you'll have to put use lib "/my/perldirectory"; near the top of the program that is to use this module.


So, I guess installing module is not possible in my system.


Please suggest script to do the below mentioned tasks.
Sample data.csv

12/27/2006,73,1100,Wed
12/27/2006,73,1200,Wed
12/28/2006,99,1200,Thu
12/28/2006,99,1300,Thu
12/28/2006,99,1400,Thu
12/29/2006,52,1400,Fri
12/29/2006,52,1500,Fri
12/30/2006,46,1700,Sat
12/30/2006,45,1800,Sat
12/31/2006,46,1800,Sun
01/01/2007,51,1200,Mon
01/01/2007,51,1200,Mon
01/02/2007,54,1200,Tue
01/02/2007,54,1200,Tue


01/03/2007 will be Wed.12/27/2006 corresponds to Wed. So,01/03/2007 will have 73,1100 corresponding to 12/27/2006
and so on for 01/04/2007.

Output outputfile.csv should have data for next 2 days
01/03/2007,73,1100,Wed
01/03/2007,73,1200,Wed
01/04/2007,99,1200,Thu
01/04/2007,99,1300,Thu
01/04/2007,99,1400,Thu

Please advise the perl script.

Thanks in advance.
Begunix
ASKER CERTIFIED SOLUTION
Avatar of Adam314
Adam314

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Begunix

ASKER

Thanks a lot.