in reply to Using HoH data in formats
Assigning the value of $_ to $cust fixes the problem...
sub printdata{ foreach (sort keys %list){ #! CHANGED $cust = $_; #! ADDED $total = $list{$cust}->{new} - $list{$cust}->{pr +ior}; write; } } __DATA__ "fred", 10 "barney", 30 "Bam bam", 5 "Wilma", 200 "Betty", 300 # Output +-----------------------------------------------------------------+ | Monthly Recap | |----------------------------+----------+-----------+-------------+ |Account | Prior | Current | Difference | |----------------------------+----------+-----------+-------------+ |Bam bam | 5.00 | 10.00 | 5.00 | |----------------------------+----------+-----------+-------------+ |Betty | 300.00 | 10.00 | -290.00 | |----------------------------+----------+-----------+-------------+ |Wilma | 200.00 | 10.00 | -190.00 | |----------------------------+----------+-----------+-------------+ |barney | 30.00 | 10.00 | -20.00 | |----------------------------+----------+-----------+-------------+ |fred | 10.00 | 10.00 | 0.00 | |----------------------------+----------+-----------+-------------+
My understanding (which is possibly wrong) is that when the format is compiled, a pointer is taken. As the iterator in a foreach loop gets aliased to the things in the list, this effectively bypasses that pointer. By doing the assignment to $cust in the body of the loop, the pointer (reference, whatever) taken when the format is compiled remains valid. That's my best speculation. With luck diotalevi will do his magic with the devel::* modules and get the real skinny.
Examine what is said, not who speaks.
|
|---|