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

Re: assigning array to hash

Thread Previous | Thread Next
From:
Shaun Fryer
Date:
April 26, 2002 15:52
Subject:
Re: assigning array to hash
Message ID:
Pine.BSO.4.21.0204261847261.31745-100000@zeus.sorcery.ca
> 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|";
}
chop($scalar_array);
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
 http://LWEB.NET
 PH:  519-858-9660
 FX:  519-858-9024
===================



Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About