A couple of hash questions

D

Dave Saville

If I have a complicated structure, say a hash of hashes/arrays etc and
I want to save it for later I can use store().

If I only have a simple hash, just plain name/value pairs, then is it
more efficient to use store() or a plain text file? Taking into account
reading the thing back in.

If the latter, is there a better way to read it back in, assuming space
separated one pair/line, than:

($name, $value) = split;
$hash{$name} = $value;

TIA

Regards

Dave Saville

NB switch saville for nospam in address
 
J

James E Keenan

Dave Saville said:
If I have a complicated structure, say a hash of hashes/arrays etc and
I want to save it for later I can use store().

If I only have a simple hash, just plain name/value pairs, then is it
more efficient to use store() or a plain text file? Taking into account
reading the thing back in.

The answer probably depends on your definition of "efficient": fastest?
smallest memory footprint? It also probably depends on the size of the hash
you're manipulating.

If it's a question of speed, the answer is probably best determined thru
benchmarking. (perldoc Benchmark)
If the latter, is there a better way to read it back in, assuming space
separated one pair/line, than:

($name, $value) = split;
$hash{$name} = $value;
Probably as good as you will get.
 
G

Gunnar Hjalmarsson

Dave said:
If I only have a simple hash, just plain name/value pairs, then is
it more efficient to use store() or a plain text file? Taking into
account reading the thing back in.

Your direct question was commented on by James.

Have you considered using DBM files?
 
D

Dave Saville

Your direct question was commented on by James.

Have you considered using DBM files?

For what I am trying to do that would be overkill - Thanks for the
suggestion.

Regards

Dave Saville

NB switch saville for nospam in address
 
G

Grendel

If the latter, is there a better way to read it back in, assuming space
separated one pair/line, than:

($name, $value) = split;
$hash{$name} = $value;

That'll work, and if you're doing something that doesn't warrant a DBM
file or such, I'll guess that the speed of how you're reading in a flat
file won't matter either.

You could do something with map, on the other hand, if you've already
got all the delimited data in an array somehow and you know there won't
by any empty values..

%hash = map { split(':',$_) } @data;
 
D

Dick Penny

Dave Saville said:
If I have a complicated structure, say a hash of hashes/arrays etc and
I want to save it for later I can use store().
I'm no guru, but I believe I saw in this posting (2-6 months ago) the same
type of Q, and it was answered by one of the gurus to the effect of use
data:dumper (or is it dump:data) and print the thing to a file,
sort of like print FILE, data:dump($hash ref); [or whatever the darn syntax
should be]

You can see my Perl is far from perfect, but the idea seemed good. I never
tried it, nor do I remember how to get it back.

HTH
Dick Pennt
 
V

Vlad Tepes

If I have a complicated structure, say a hash of hashes/arrays etc and
I want to save it for later I can use store().

Yes. There are a number of modules you could use for this, including
Storable and Data::Dumper. Attribute::persistent is a cool one you
might want to check. You could store it in a database with MLDBM.
If I only have a simple hash, just plain name/value pairs, then is it
more efficient to use store() or a plain text file? Taking into account
reading the thing back in.

The difference in speed probably is insignificant unless you have
a lot of data. But don't take my word for it, check for yourself. If
you wonder about performance of different methods, it's time to
learn how to use the Benchmark module. The pods contains examples,
and the module is easy to use.
If the latter, is there a better way to read it back in, assuming space
separated one pair/line, than:

($name, $value) = split;
$hash{$name} = $value;

split() is fine. Another way is using a capturing regex. If the data
is in fixed-width columns you could use unpack() or substr().

--
(,_ ,_, _,)
/|\`\._( )_./'/|\
· · \/ L /\ D · ·
/__|.-'`-\_/-`'-.|__\
`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·.. ` " `
 
G

Gunnar Hjalmarsson

Dave said:
For what I am trying to do that would be overkill

That made me curious, Dave.

I have never used Storable, but I'm storing a few small hashes in SDBM
files. I have never benchmarked, but it seems to be more durable than
plain text files, and the minimum size of an SDBM database is just 1 kiB.

What is it that makes you say it would be overkill?
 
D

Dave Saville

That'll work, and if you're doing something that doesn't warrant a DBM
file or such, I'll guess that the speed of how you're reading in a flat
file won't matter either.

You could do something with map, on the other hand, if you've already
got all the delimited data in an array somehow and you know there won't
by any empty values..

%hash = map { split(':',$_) } @data;

Now that looks interesting - slurp the lot into an array and build the
hash out of the array. As long as I've enough memory :) But I *think*
the file will only grow to 40K ish. Each pair is only 20 characters.

Time for playing I think - Thanks to those who pointed out benchmark.

Regards

Dave Saville

NB switch saville for nospam in address
 
D

Dave Saville

That made me curious, Dave.

I have never used Storable, but I'm storing a few small hashes in SDBM
files. I have never benchmarked, but it seems to be more durable than
plain text files, and the minimum size of an SDBM database is just 1 kiB.

What is it that makes you say it would be overkill?

I am only talking a couple of thousand pairs - and I don't have a DB
installed - at least not on this box and in any case I might be
releasing the code to a few friends and I can be sure they won't - a
plain file or a perl internal type file would be portable code.

Regards

Dave Saville

NB switch saville for nospam in address
 
G

Gunnar Hjalmarsson

Dave said:
I am only talking a couple of thousand pairs - and I don't have a
DB installed - at least not on this box and in any case I might be
releasing the code to a few friends and I can be sure they won't -
a plain file or a perl internal type file would be portable code.

Simple DBM files (SDBM) are portable, at least between *nix and
Windows platforms. SDBM_File.pm, which gives you tied access to such
files, comes with Perl, and does not require any separate database
program.

I'm not saying it's better, but I still believe it would be an
alternative to you.
 
D

Dave Saville

Simple DBM files (SDBM) are portable, at least between *nix and
Windows platforms. SDBM_File.pm, which gives you tied access to such
files, comes with Perl, and does not require any separate database
program.

I'm not saying it's better, but I still believe it would be an
alternative to you.

OK I will look into it. Thanks.

Regards

Dave Saville

NB switch saville for nospam in address
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top