#!/usr/bin/perl use strict; use warnings; # 819634 my $file = "checkdata.csv"; # open( F, "$file" ); #replaced: see Note 1 open(F, '<', $file) or die "Can't open $file for read: $!"; my @lines = ; my $sum = 0; my ( $vplus, $v2 ); for my $line ( @lines ) { my $minus_sign = 0; my $fractional_pennies = 0; my @data = ''; chomp( $line ); @data = split( ",", $line ); if ( $data[2] =~ /\s*(-)(\d*\.\d{2})(\d?)/ ) # note 2 { $minus_sign = $1; $v2 = $2; $fractional_pennies = $3; if ( $fractional_pennies =~ /[^0]/ ) { warn "\n\t fractional pennies present in: " . $v2,$fractional_pennies . "\n\t Ignoring the fraction.\n"; } } elsif ( $minus_sign ne "-" ) { warn "\n\t Missing minus sign in $data[2]\n\t Continuing as if it were present"; if ( $data[2] =~ /\s*(-){0}(\d*\.\d{2})(\d?)/ ) # Note 3 { $v2= $2; } } print " \$v2 is: $v2\n"; $sum = $sum + $v2; } printf("Total: %5.2f\n", $sum); #### "Lunch Place", 1/22/2010, -4.110000, "Lunch", "", , 0 "SomewhereElse", 1/23/2010, -17.40, "Beer", "12345", 0 "Lunch Place", 1/25/2010, -4.755, "Lunch", 0 # fractional pennies: highly suspect "Yet Another Joint", 1/24/2010, 13.240000, "Lunch", 0 # not a negative amount: data entry error? #### $v2 is: 4.11 $v2 is: 17.40 fractional pennies present in: 4.755 Ignoring the fraction. $v2 is: 4.75 Missing minus sign in 13.240000 Continuing as if it were present at F:\_wo\pl_test\819634.pl line 34, line 4. $v2 is: 13.24 Total: 39.50