in reply to Using HoH data in formats
What I can't seem to get a handle on is if it is possible to use the HoH variables in the format statement or do I have to use temporary variables?
The problem isn't with using your HoH variables in the format statement, as you'll notice that your prior and new fields are getting a value. The problem is with your $cust variable. As far as I can tell, the format watches each time that the $cust variable is set explictly and maintains that value somehow. Since assignment ($cust = 1) is different to aliasing (foreach $cust (@list)) in some way, the last assigned value to $cust (ie the last customer read in) is the value that the format keeps.
I think that there might be a special variable that tells the format to reset all its notions of what things are but you don't really need to use it here.
This code will do what you want it to:
Hope it helps.my %list; my ($customer, $total); # use $custome +r instead of $cust format STDOUT_TOP = +-----------------------------------------------------------------+ | Monthly Recap | |----------------------------+----------+-----------+-------------+ |Account | Prior | Current | Difference | |----------------------------+----------+-----------+-------------+ . format STDOUT = |@<<<<<<<<<<<<<<<<<<<<<<<<<<<|@#####.## | @#####.## | @#####.## | $customer, $list{$customer}->{prior}, $list{$customer}->new, $total |----------------------------+----------+-----------+-------------+ . &readfile('sfoct01.txt', 'prior'); # &readfile('sfnov01.txt', 'new'); &printdata; sub readfile{ my ($file, $money) = @_; my ($line, $amount); die unless open FH, $file; while ($line = <FH>){ chomp($line); my ($cust, $amount) = split('",',($line)); # localise thi +s $cust $cust =~ s/"//g; if (exists $list{$cust}){ $list{$cust}->{$money} += $amount; } else{ $list{$cust}->{$money} = $amount; } $list{$cust}->{new} = 10; } close FH; } sub printdata{ foreach my $cust (sort keys %list){ # localised +$cust $customer = $cust; # assign to +$customer $total = $list{$cust}->{new} - $list{$cust}->{pri +or}; write; } }
jarich
Update: Thought it might help to make the differences more explicit.
|
|---|