Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving hashes for later use 4

Status
Not open for further replies.

torstens

Technical User
Oct 9, 2006
26
0
0
US
I'm wondering how the people of this community will save their hash variables for permanent use. I've saved my arrays to text files pretty well, but I don't know how I would go about printing a hash to a text file, closing the programs, and later extracting the saved hash text back into a hash variable.
 
how does that work kevin?

I've had a read and this example is throwing me a bit...

Code:
 use Storable;
 store \%table, 'file';
 $hashref = retrieve('file');

so does $hashref->{'key'} work the way you would normally reference a hash.


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Thanks for the tip. A couple of questions:

I don't see where I actually download it from that page.

Also, do you (or anyone reading this) have any experience with the other "Storable" modules found on cpan.org. There are a couple of newer ones written by a couple of different people. Are they the same? Is any better than the others?
 
The basic Storable module is a core module, it should already be installed with your current perl installation. I don't know if any of the other storable modules are better than the one I mentioned.

- Kevin, perl coder unexceptional!
 
@1DMF: With that example, $hashref will effectively be a reference to %table, so $hashref->{something} is the same as $table{something}.

If course it's not *actually* a reference to %table, so changing one won't change the other. It's a reference to a copy of %table.
 
>> so does $hashref->{'key'} work the way you would normally reference a hash.

Yes.



- Kevin, perl coder unexceptional!
 
right, but if you stored the hash to the file, the script ends, then another script retrives the hash, i take it you would only then be working with the retrieved reference and %table wouldn't exist.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
>> right, but if you stored the hash to the file, the script ends, then another script retrives the hash, i take it you would only then be working with the retrieved reference and %table wouldn't exist.

That is correct. But that is alo the intended purpose of the Storable module: persistence of data. It stores data structures that you might typically lose when a script ends so you can reuse/share them.

- Kevin, perl coder unexceptional!
 
I guess you could use JSON/XML/CSV etc.. for the same purpose and create your own text files of the data.

neat little module though, just what i love about the TT PERL forum, learn something new everyday.

One day I might even get my head round OOP , but i think that's gonna take a while :)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Yes you could store data structures in any number of ways, but with the Storable module it's as easy as it gets. I'm still wrapping ny head around OO too so don't feel bad.

- Kevin, perl coder unexceptional!
 
What am I doing wrong? I'm playing with Storable, and I was hoping to create an array (@test) which I retrieve from a file, then push another array (@addon) onto it, and then resave it. The hope is that every time I start this program I'll get...

first first first added added

and the next time

first first first added added added added

and so on.



use Storable qw(store retrieve freeze thaw dclone);

@test = qw(first first first);
print "@test\n";

@addon = qw(added added);
print "@addon\n";

store(\@test, 'mytest') or die "Can't store\n";

@retrieved_test = retrieve('mytest');

@test = push @retrieved_test, @addon;

store (\@test, 'mytest') or die "Can't store\n";



print "@test\n";


<STDIN>;
 
you're OK up to here:

Code:
@test = qw(first first first);
print "@test\n";

@addon = qw(added added);
print "@addon\n";

store(\@test, 'mytest') or die "Can't store\n";

after that you have some problems. This line:

Code:
@retrieved_test = retrieve('mytest');

should really be a reference to the array, but you can do it like you had it for your simple array, here it is as a reference:

Code:
$retrieved_test = retrieve('mytest');

this line is using the push() function incorrectly:

Code:
@test = push @retrieved_test, @addon;

@test will now be the number of elements in the array (@retrieved_test). All the original values of @test are gone at this point because you over-wrote the array by using the assignment operator '='.

You want something more along these lines:

Code:
@test = qw(first first first);
print "@test\n";

@addon = qw(added added);
print "@addon\n";

store(\@test, 'mytest') or die "Can't store\n";

$retrieved_test = retrieve('mytest');

push @{$retrieved_test}, @addon;

store ($retrieved_test, 'mytest') or die "Can't store\n";

print "@{$retrieved_test}\n";

You should stick to using a reference of the returned data from the retrieve() function because more complex data structures (a hash of hashes for example) may not dereference as cleanly as a simple array.

- Kevin, perl coder unexceptional!
 
Firstly, you're storing a reference to an array (\@test) and then trying to retrieve that into an array. @retrieved_test will only have one element: a reference to an array.

Also, I'm not sure what you're doing with this code:
Code:
@test = push @retrieved_test, @addon;
You're adding the elements of @addon to @retrieved_test and then storing the number of elements of @retrieved_test in @test as the only element in @test.

I think this is what you're looking for:
Code:
use Storable qw(store retrieve freeze thaw dclone);


my @test = qw(first first first);
print "@test\n";

my @addon = qw(added added);
print "@addon\n";

store(\@test, 'mytest') or die "Can't store\n";

my @retrieved_test = @{ retrieve('mytest') };

push @retrieved_test, @addon;

store (\@retrieved_test, 'mytest') or die "Can't store\n";

print "@retrieved_test\n";
 
>> Firstly, you're storing a reference to an array (\@test) and then trying to retrieve that into an array. @retrieved_test will only have one element: a reference to an array.

I thought the array would just be dereferenced properly, but it actually returns this:

first first first added added ARRAY(0x155ad00)

it returns the array with the reference tagged onto the end.

- Kevin, perl coder unexceptional!
 
Thanks a lot, It helps to have the complex and the simple problems explained. The final program I made is tweeked just a bit, but it gets the point across that you can have permanence. Just for the completeness of this post I'm including it below.

use Storable qw(store retrieve freeze thaw dclone);


my @test = @{ retrieve('mytest') };
print "@test\n";

my @addon = qw(added);
print "@addon\n";

store(\@test, 'mytest') or die "Can't store\n";

my @retrieved_test = @{ retrieve('mytest') };

push @retrieved_test, @addon;

store (\@retrieved_test, 'mytest') or die "Can't store\n";

print "@retrieved_test\n";

<STDIN>;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top