I got a little confused about that, because in the section of the perlreftut that discuss the "Arrow Rule" it says the arrow isn't necessary between two subscripts. I guess that means it's required in this case? If $reports{$year} and $reports->{$year} are different variables, and $reports->{$year} is a hash reference, then what is $reports{$year}?

In any event, I've added use strict; and your printit sub, and now the "new data" is identical to the "old data". Here's the code:

#!/usr/bin/perl use Storable qw(lock_nstore lock_retrieve); use strict; my $reports; my %reports; my $label; # 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) { my @myyears = (2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 201 +0, 2011); my $fiscal_year = $myyears[int(rand 9)]; my @myquarters = ('Q1', 'Q2', 'Q3', 'Q4'); my $qua = $myquarters[int(rand 3)]; my @myfiles = ('foo.txt', 'bar.dat', 'bap.zip', 'baz.tgz', 'zif.do +c', 'blu.shn', 'sng.mp3', 'zap.pif', 'fiz.pdf', 'shz.nit'); my $filen = $myfiles[int(rand 9)]; my @mytitles = ('fooey', 'dooey', 'looey', 'zooey', 'gooey', 'hooe +y', 'tooey', 'pooey', 'rooey', 'blooey'); my $title = $mytitles[int(rand 9)]; my @mydescriptions = ('desc1', 'desc2', 'desc3', 'desc4', 'desc5', + 'desc6', 'desc7', 'desc8', 'desc9', 'desc10'); my $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 { my $year; my $quarter; my $file; # print the hash print "\n======== BEGIN $label =========\n"; foreach my $year ( sort keys %{$reports} ) { print "$year: \n"; foreach my $quarter(sort keys %{$reports->{$year}}){ print " $quarter: \n"; foreach my $file (sort keys %{$reports->{$year}{$quarter} +} ) { print " Filename: $file\n"; print " Title: $reports->{$year}{$quarter}{$f +ile}{title}\n"; print " Description: $reports->{$year}{$quarter}{$f +ile}{description}\n\n"; } } } print "\n======== END $label =========\n"; }

Still stumped! I'm going to write another version using Data::Dumper just for fun, unless anyone sees any caveats with that approach for this data structure, but I'd also really like to know how to do this with Storable.

Thanks for your help!


In reply to Re: Re: Storable with hash of hashes (reference problem?) by ManyCrows
in thread Storable with hash of hashes (reference problem?) by ManyCrows

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.