in reply to Problem subtracting in Perl

Off the top of my head, I don't see your problem right away. This could be an issue with the data or an issue with the code. The interesting thing, though, is that problems like this can often be spotted quickly if the code is developed well. Let's take a look at what you have (I've cleaned up the formatting to clarify scope:

01: foreach $line(<FILE>) 02: { 03: chomp($line); 04: $line =~ s/"| //g; 05: @record = split(/,/,$line); 06: 07: #### The custhash is created from an index file. 08: if(!defined $custhash{$record[0]}[0]) 09: { 10: print "$line\n"; 11: next; 12: } 13: 14: $salesman = $custhash{$record[0]}[0]; 15: $custnum = $record[0]; 16: 17: #### The next 2 lines are not producing the expected result! 18: $hashlist{$salesman}{$custnum}[6] += $record[5]; 19: $hashlist{$salesman}{$custnum}[6] -= $record[7]; 20: 21: ##### These 3 lines I used for debug and the results are ??? 22: $orders += $record[5]; 23: $shipments += $record[7]; 24: $openorders += $hashlist{$salesman}{$custnum}[6]; 25: 26: #on hand inventory 27: $hashlist{$salesman}{$custnum}[8] += $record[6]; 28: 29: @duedate = split(/\//,$record[16]); 30: ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtim +e; 31: 32: if(($duedate[0] == ($mon+1)) && ($duedate[2] == ($year-100))) 33: { 34: #due this month 35: $hashlist{$salesman}{$custnum}[7] += $record[9]; 36: } 37: } 38: close (FILE);

Line 4: why are we substituting out quote marks? If they're in your incoming data, this suggests that you might have embedded commas. You're better off with a module like Text::CSV_XS to handle CSV files. If you have a lot of files like this, you can use my CSV Database Validation code to check their integrity.

Line 8: what is $record[0]? It turns out that it's actually $custnum, as defined on line 15. Move line 15 above line 8 and use that variable. Your code will be more self-documenting. Further, I see that you didn't check to if the customer number exists in the hash. If it doesn't, do you want to autovivify this entry?

Lines 18 and 19: these are your problem. However, I have no idea what that data really is. Part of the problem stems from your assignment on line 5: @record = split(/,/,$line);. You might want to consider assigning to individual named scalars (or to a hash) so that you have have self-documenting code. Just a bunch of numeric indices gives the programmer no clue as to what is going on. Further, since you are not validating any of the data, how can you tell if you have any data corruption issues?

The last point is the obvious one: you're not using strict and I suspect you are not using warnings, either. Because you're not using strict, it's very easy for all sorts of nasty bugs to slip unnoticed in your code. Further, since you aren't predeclaring your variables with my, this means that they are global and you may have scoping issues that could trip you up. Is it possible that this code is called more than once and you have some leftover data creeping in?

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.