in reply to Re: copying records from one file to another with filter.
in thread copying records from one file to another with filter.

#!/usr/bin/perl use strict; use warnings; use Switch; my $srcdir = "D:/source/"; my $srcfile = $srcdir."money.dat"; open (READ, "< $srcfile") || die "Can't find the dat file\n"; my ($date, $expense, $income); my $epochToday = time; my $last30days = $epochToday - 2592000; my ($year, $month, $day) = (localtime($last30days))[5,4,3]; $month++; $year+=1900; my $mon; switch ($month) { case [1] {$mon="Jan"} case [2] {$mon="Feb"} case [3] {$mon="Mar"} case [4] {$mon="Apr"} case [5] {$mon="May"} case [6] {$mon="Jun"} case [7] {$mon="Jul"} case [8] {$mon="Aug"} case [9] {$mon="Sep"} case [10] {$mon="Oct"} case [11] {$mon="Nov"} case [12] {$mon="Dec"} } my $lastdate = $day."-".$mon."-".$year; my $read; my $total; while (<READ>) { chop; ($date, $expense, $income) = split(/,/,$_); $total = $expense+$income; @data_date = ($date); MY PROBLEM STARTS HERE!!!!! if($lastdate =~ /^@data_date/) { } @data = ($total); push @data, [@data_date]; push @recent, [@data]; $read++; } close READ || die "Couldn't close the dat file"; my $desdir = "D:/target/"; my $desfile = $desdir."total.csv"; open (WRITE, "< $desfile") || die "Can't find the csv file.\n"; my $y=0; foreach(@recent) { $printout .= "$recent[$y][0],$recent[$y][1]\n"; $y++; } print WRITE $printout; close WRITE || die "Couldn't close the csv file.\n"; exit ;

Replies are listed 'Best First'.
Re^3: copying records from one file to another with filter.
by ww (Archbishop) on Jan 09, 2010 at 17:36 UTC

    Actually, your problem started when you didn't check your script with perl -c scriptname. Had you done so, you would have seen this:

    >perl -c \816493.pl Possible unintended interpolation of @data_date in string at 816493.pl + line 60. Global symbol "@data_date" requires explicit package name at 816493.pl + line 56. syntax error at 816493.pl line 60, near "if" Global symbol "@data_date" requires explicit package name at 816493.pl + line 60. Global symbol "@data" requires explicit package name at816493.pl line +65. Global symbol "@data" requires explicit package name at 816493.pl line + 67. Global symbol "@data_date" requires explicit package name at 816493.pl + line 67. Global symbol "@recent" requires explicit package name at816493.pl lin +e 68. Global symbol "@data" requires explicit package name at 816493.pl line + 68. syntax error at 816493.pl line 71, near "}" 816493.pl had compilation errors.

    IE, what you've posted won't compile. Suggest you strip this down to the bare essentials. Then, when you have a compilable version that illustrates your problem, you'll be ready to post that.

      Hi, as I said im new to perl so dont kno much. but thanks anyways. also Im tryin to write this code for some ETL purpose, not for IE or any browser.
Re^3: copying records from one file to another with filter.
by Corion (Patriarch) on Jan 09, 2010 at 22:17 UTC

    Your code has at least one large problem:

    use Switch;

    This module is a bad idea and not recommended for use, even by its author anymore. Remove it and the switch construct. If you use Perl 5.10, use the new given keyword, otherwise use the approach you already got for the switch expression or use a chain of if ... elsif ... elsif statements.

Re^3: copying records from one file to another with filter.
by zwon (Abbot) on Jan 09, 2010 at 17:39 UTC

    You could replace

    switch ($month) { case [1] {$mon="Jan"} case [2] {$mon="Feb"} case [3] {$mon="Mar"} case [4] {$mon="Apr"} case [5] {$mon="May"} case [6] {$mon="Jun"} case [7] {$mon="Jul"} case [8] {$mon="Aug"} case [9] {$mon="Sep"} case [10] {$mon="Oct"} case [11] {$mon="Nov"} case [12] {$mon="Dec"} }
    with
    $mon = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$month-1] +;
      Thanks a lot for the suggestion.