Inspired by 3d array of hashes I've built up a data structure that works well for storing some hierarchical data. Now I'm attempting to store and retrieve it using Storable. Unfortunately my script is either not storing all my data, or it just isn't printing it correctly. I suspect that this is all a result of my still developing understanding of how references work, but I'm really not sure.
Here's the code, anyone know what I'm missing?
#!/usr/bin/perl use Storable qw(lock_nstore lock_retrieve); # load the file if (-e "reporting.storable") { $reports = lock_retrieve("reporting.storable") || die "Couldn't lock_retrieve reporting.storable: $!\n"; } $label = 'old data'; printit(); # generate some random data for (0 .. 10) { @myyears = (2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, +2011); $fiscal_year = $myyears[int(rand 9)]; @myquarters = ('Q1', 'Q2', 'Q3', 'Q4'); $qua = $myquarters[int(rand 3)]; @myfiles = ('foo.txt', 'bar.dat', 'bap.zip', 'baz.tgz', 'zif.doc', + 'blu.shn', 'sng.mp3', 'zap.pif', 'fiz.pdf', 'shz.nit'); $filen = $myfiles[int(rand 9)]; @mytitles = ('fooey', 'dooey', 'looey', 'zooey', 'gooey', 'hooey', + 'tooey', 'pooey', 'rooey', 'blooey'); $title = $mytitles[int(rand 9)]; @mydescriptions = ('desc1', 'desc2', 'desc3', 'desc4', 'desc5', 'd +esc6', 'desc7', 'desc8', 'desc9', 'desc10'); $description = $mydescriptions[int(rand 9)]; # update the data $reports{$fiscal_year}{$qua}{$filen}->{title} = "$title"; $reports{$fiscal_year}{$qua}{$filen}->{description} = "$descriptio +n"; } $label = 'new data'; printit(); # save the file $reports = \%reports ; lock_nstore ($reports, "reporting.storable") || die "Couldn't lock_nstore reporting.storable: $!\n"; sub printit { # print the hash print "\n======== BEGIN $label =========\n"; foreach $year ( sort keys %{$reports} ) { print "$year: \n"; foreach $quarter ( sort keys %{ $reports{$year} } ) { print " $quarter: \n"; foreach $file ( sort keys %{ $reports{$year}{$quarter} } ) + { print " Filename: $file\n"; print " Title: $reports{$year}{$quarter}{$fil +e}{title}\n"; print " Description: $reports{$year}{$quarter}{$fil +e}{description}\n\n"; } } } print "\n======== END $label =========\n"; }
In reply to Storable with hash of hashes (reference problem?) by ManyCrows
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |