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

Re: assigning array to hash

Thread Previous | Thread Next
April 27, 2002 09:15
Re: assigning array to hash
Message ID:

On Friday, April 26, 2002, at 03:49 , Shaun Fryer wrote:
> 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.

well played!

Thought I would benchmark this against the more traditional
'join' strategy - and the two basic ways I use for passing
around arrays in hashes...

This did give me the chance to look at the various ways that this
could be done - and I shall leave it to others to resolve which
variation on a theme would be more consistent with their code
maintenance issues....

but you might want to look at

sub joinFunc {
	my $scalar_array = join('|',@array);
	$hash{$some_Other_key} = $scalar_array;
	my @new_array = split(/\|/, $hash{$some_Other_key});
} # end of joinFunc

as a slightly more efficient strategy....



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