Re: perl file formatting
by hippo (Bishop) on Dec 23, 2016 at 09:41 UTC
|
| [reply] |
Re: perl file formatting
by poj (Abbot) on Dec 23, 2016 at 09:30 UTC
|
#!perl
use strict;
use Time::Piece;
use Time::Seconds 'ONE_DAY';
my $process_date = '20161002';
my $t = Time::Piece->strptime($process_date,"%Y%m%d");
#my $t = Time::Piece->new(); # today
print "Process date is ".$t->ymd('')."\n";
my $data_date = '20160930';
my $d = Time::Piece->strptime($data_date,"%Y%m%d");
print "Date in data is ".$d->ymd('')."\n";
$d += ONE_DAY;
while ($d <= $t){
print " Add date ".$d->ymd('')."\n";
$d += ONE_DAY;
}
poj
| [reply] [d/l] |
Re: perl file formatting
by Corion (Patriarch) on Dec 23, 2016 at 08:39 UTC
|
What code have you already written and where are you encountering problems?
I assume that your input data will either be space delimited or fixed width, so split or unpack might be of help. For bringing the data in date order, sort will likely help. For filtering the data for the report run date, see grep.
Usually, it helps us help you much better if you post an SSCCE as a starting point for the discussion. That shows us where you are and allows us to give you much better directions on how to get to where you want to be.
| [reply] |
Re: perl file formatting
by tybalt89 (Monsignor) on Dec 23, 2016 at 17:34 UTC
|
#!/usr/bin/perl
# http://perlmonks.org/?node_id=1178418
use strict;
use warnings;
sub eightdigitdate
{
my @fields = localtime(shift);
sprintf '%04d%02d%02d', $fields[5] + 1900, $fields[4] + 1, $fields[3
+];
}
sub nextday
{
use Time::Local;
shift() =~ /(\d{4})(\d\d)(\d\d)/;
eightdigitdate( 60*60*24 + timelocal 0, 0, 12, $3, $2 - 1, $1 );
}
#my $today = eightdigitdate(time);
my $today = '20161002';
print scalar <DATA>;
print my @data = <DATA>;
my $more = 1;
while($more)
{
for(@data)
{
my $date = (split)[1];
if( $date < $today )
{
s/\b$date\b/ nextday($date) /e;
print;
}
else
{
$more = 0;
}
}
}
__DATA__
F12 F11 F481 F55 F989
8204 20160930 1064.848 test Y
8204A 20160930 1064.505 test Y
8204B 20160930 1045.469 test Y
| [reply] [d/l] |
|
Tried something like this But couldnt get the required ,Any help APPRECIATED
#!/usr/bin/perl
my $isHeader = 1;
my $targetDate = 20161002;
foreach (<STDIN>){
if (!$isHeader){
my ($M12,$M11,$M481,$M55,$M989) = split(' ',$_);
if ($M11 <= $targetDate){
print $_;
}
}
$isHeader = 0;
}
| [reply] [d/l] |
|
split(/\s+/, $_);
Hope this helps!
The way forward always starts with a minimal test.
| [reply] [d/l] |
|
|
|
|
#!/usr/bin/perl
# http://perlmonks.org/?node_id=1178418
use strict;
use warnings;
sub eightdigitdate
{
my @fields = localtime(shift);
sprintf '%04d%02d%02d', $fields[5] + 1900, $fields[4] + 1, $fields[3
+];
}
sub nextday
{
use Time::Local;
shift() =~ /(\d{4})(\d\d)(\d\d)/;
eightdigitdate( 60*60*24 + timelocal 0, 0, 12, $3, $2 - 1, $1 );
}
#my $today = eightdigitdate(time);
my $today = '20161002';
print scalar <STDIN>;
print my @data = <STDIN>;
my $more = 1;
while($more)
{
for(@data)
{
my $date = (split)[1];
if( $date < $today )
{
s/\b$date\b/ nextday($date) /e;
print;
}
else
{
$more = 0;
}
}
}
| [reply] [d/l] |
|
Re: perl file formatting
by Marshall (Canon) on Dec 23, 2016 at 09:06 UTC
|
Your post is incomprehensible due to the formatting.
Please enclose fixed field stuff within <code>...</code> tags. That will show where one line ends and another begins.
See Markup in the Monastery for description of other tags.
Update: I see that you updated the code tags. Thanks!
Another Perl module to consider is Date::Time. | [reply] [d/l] |