develooper Front page | perl.beginners | Postings from April 2002

Re: assigning array to hash

Thread Previous | Thread Next
Shaun Fryer
April 26, 2002 15:52
Re: assigning array to hash
Message ID:
> Below is part of some code that reads from a file to an array and then
> attempts to assign the array to a hash.  I get no compiling errors, but
> %label_hash is empty. No doubt I am being stupid. Has anyone a
> suggestion how to fill the hash variable?

	Well, I'm not totally certain what you're trying to
accomplish, but here's an idea of something I frequently do along the
same lines.

foreach $value (@array) {
  $scalar_array .= "$value|";
my %hash;
$hash{some_key} = $scalar_array;

	Then if you want to get the array from the key's value later
on, you just split(/\|/,"$hash{some_key}"). I typically do this when
writing berkley db's which need to have variable length arrays as the
values of a list of keys. You'll have to pick a delimiter that won't
exist within $value.

 Shaun Fryer
 London Webmasters
 PH:  519-858-9660
 FX:  519-858-9024

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About