Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > A couple of hash questions

Reply
Thread Tools

A couple of hash questions

 
 
Dave Saville
Guest
Posts: n/a
 
      09-28-2003
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


 
Reply With Quote
 
 
 
 
James E Keenan
Guest
Posts: n/a
 
      09-28-2003

"Dave Saville" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ews.ntlworld.com...
> 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.


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      09-28-2003
Dave Saville wrote:
> 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?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
Dave Saville
Guest
Posts: n/a
 
      09-28-2003
On Sun, 28 Sep 2003 20:29:52 +0200, Gunnar Hjalmarsson wrote:

>Dave Saville wrote:
>> 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?


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


 
Reply With Quote
 
Grendel
Guest
Posts: n/a
 
      09-28-2003
In article <(E-Mail Removed). ntlworld.com>,
Dave Saville wrote:

> 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;



--
| grendel at durge dot org | web technologist | london, uk
| "It's people like you what cause unrest"
 
Reply With Quote
 
Dick Penny
Guest
Posts: n/a
 
      09-29-2003

"Dave Saville" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ews.ntlworld.com...
> 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



 
Reply With Quote
 
Vlad Tepes
Guest
Posts: n/a
 
      09-29-2003
On the CCLXXII'nd day of the MMIII'rd year, Dave Saville spoketh:

> 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:umper. Attribute:ersistent 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
/__|.-'`-\_/-`'-.|__\
`..`..`..`..`..`..`.. ` " `
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      09-29-2003
Dave Saville wrote:
> On Sun, 28 Sep 2003 20:29:52 +0200, Gunnar Hjalmarsson wrote:
>> Dave Saville wrote:
>>> 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.

>>
>> Have you considered using DBM files?

>
> 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?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
Dave Saville
Guest
Posts: n/a
 
      09-29-2003
On 28 Sep 2003 20:42:28 GMT, Grendel wrote:

>In article <(E-Mail Removed). ntlworld.com>,
>Dave Saville wrote:
>
>> 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;


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


 
Reply With Quote
 
Dave Saville
Guest
Posts: n/a
 
      09-29-2003
On Mon, 29 Sep 2003 06:57:18 +0200, Gunnar Hjalmarsson wrote:

>> 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?


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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
A couple of T-Bird questions Dale Brisket Firefox 2 10-02-2005 10:25 PM
Newbie questions - Couple of VC++ questions regarding dlls and VB6 Ali Syed C Programming 3 10-13-2004 10:15 PM
1721 ADLS - a couple of questions ? Thomas Cisco 0 01-30-2004 04:42 PM



Advertisments