Why am I getting unexpected output trying to write a hash structure to a file?
- by Harm De Weirdt
I have a hash in which I store the products a customer buys (%orders). It uses the product code as key and has a reference to an array with the other info as value.
At the end of the program, I have to rewrite the inventory to the updated version (i.e. subtract the quantity of the bought items)
This is how I do rewrite the inventory:
sub rewriteInventory{
open(FILE,'>inv.txt');
foreach $key(%inventory){
print FILE "$key\|$inventory{$key}[0]\|$inventory{$key}[1]\|$inventory{$key}[2]\n"
}
close(FILE);
}
where $inventory{$key}[x] is 0 → Title, 1 → price, 2 → quantity.
The problem here is that when I look at inv.txt afterwards, I see things like this:
CD-911|Lady Gaga - The Fame|15.99|21
ARRAY(0x145030c)|||
BOOK-1453|The Da Vinci Code - Dan Brown|14.75|12
ARRAY(0x145bee4)|||
Where do these ARRAY(0x145030c)||| entries come from? Or more important, how do I get rid of them?