A few observations and comments:

You are reading in the entire file and saving it in an array of arrays, but then don't do anything with it. (Well, other than use the size of the array as a never terminating while() condition.) For that situation your loop probably should be

for (@input)
rather than
while (@input)

You are searching for a YYYY-MM-DD pattern when your example data is in DD-MMM-YYYY format. You'll never find anything.

You should probably use the 3 argument form of open and lexical file handles. What you have isn't wrong particularly, but isn't considered best practices.

The way you are calculating 30 days is easy but fragile. You may be better off using one of the date calculation packages. It may be good enough here though, so take that with a grain of salt.

Here's a minor reworking of your posted script to be more efficient (and work, for certain values of "work").

#!/usr/bin/perl use strict; use warnings; my @months = ( qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/); # source file directory my $srcdir = '../source'; # source file name my $srcfile = $srcdir . '/vol.dat'; my $desdir = '../target'; my $desfile = $desdir . '/total_volume.csv'; # Open source file. open my $read, '<', $srcfile or die "Can't open the DAT file $!\n"; open my $write, '>', $desfile or die "Can't open the CSV file. $!\n"; print $write "Date,Total_Volume\n"; my ( $year, $month, $day ) = ( localtime( time() - 2592000 ) )[ 5, 4, +3 ]; $year += 1900; my $startdate = "$day-".$months[$month]."-$year"; while (<$read>) { if ( /^$startdate/ .. /\x4/ ) { chomp; my (@items) = split /,/; printf $write "%s,%d\n", $items[0], $items[1] + $items[2]; } } # probably not necessary but not a bad idea close $read or die "Couldn't close the DAT file: $!\n"; close $write or die "Couldn't close the CSV file: $!\n";

In reply to Re^3: copying records from one file to another with filter. by thundergnat
in thread copying records from one file to another with filter. by avanta

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.