Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I ran into this problem, and was very frustrated with the provided examples in getting this to work correctly. Especially on the retrieve, because I could not get the foreach/keys loop to work with the hash after the retrieve. The hash would populate, and could manually plug in the hash of hash values, but, it would not work with the foreach/keys loop. So a peer of mine suggested I try converting the hashref to a hash via %$ construct. I had not seen this before. It worked! After a few trials looking at the hash with Data::Dumper, I was able to get this working using only the Storable module. So now the retrieve side works with the foreach/keys loop. I also provided a JSON example.

Yeah !!!

#/usr/bin/perl # script 1 use Storable; my %eTAG,$i; $eTAG{'Truck'}{'Fuel'}="Gas"; $eTAG{'Truck'}{'mpg'}=15; $eTAG{'SUV'}{'Fuel'}="Gas"; $eTAG{'SUV'}{'mpg'}=21; #for illustration only foreach $i (keys %eTAG){ print "$i\n"; } store \%eTAG, 'fileHash.dat'; exit 0;

Script #2

#!/usr/bin/perl # Script #2 use Storable; my $i, $href = retrieve('fileHash.dat',{binmode=>':raw'}); my %eTAG = %$href; #This was the key to getting it working. foreach $i (keys %eTAG) { print "$i\n"; } exit 0;

JSON version

#/usr/bin/perl # script 1 JSON version use JSON; my %eTAG,$i; $eTAG{'Truck'}{'Fuel'}="Gas"; $eTAG{'Truck'}{'mpg'}=15; $eTAG{'SUV'}{'Fuel'}="Gas"; $eTAG{'SUV'}{'mpg'}=21; #for illustration only foreach $i (keys %eTAG){ print "$i\n";} my $JSONdata = encode_json(\%eTAG); open(OFIL,">fileHash.dat"); print OFIL $JSONdata; close(OFIL); exit 0;
#!/usr/bin/perl # Script #2 JSON version use JSON; my $i, $href,$JSONdata; open(IFIL,"<fileHash.dat"); $JSONdata= <IFIL>; close(IFIL); $href = decode_json($JSONdata); my %eTAG = %$href; #This was the key to getting it working. foreach $i (keys %eTAG) { print "$i $eTAG{$i}{'mpg'}\n"; } exit 0;

In reply to Re: How to save and reload my hash by dcronin135
in thread How to save and reload my hash by lepetitalbert

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-23 23:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found