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

Re: assigning array to hash

Thread Previous | Thread Next
From:
drieux
Date:
April 27, 2002 09:15
Subject:
Re: assigning array to hash
Message ID:
E8B3F70B-59F9-11D6-81C5-0030654D3CAE@wetware.com

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

http://www.wetware.com/drieux/CS/lang/Perl/Beginners/BenchMarks/loopVjoin.
txt

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

ciao
drieux

---


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