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

Re: assigning array to hash

Thread Previous | Thread Next
From:
Chas Owens
Date:
April 29, 2002 08:35
Subject:
Re: assigning array to hash
Message ID:
1020094157.3841.75.camel@tert.icallinc.com
On Sat, 2002-04-27 at 12:14, drieux wrote:
> 
> 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!

TMTOWTDI:

my @array = qw(first second last);
my %hash;
#the $" var controls what characters separate elements of
#an interpolated array 
{ local $" = '|'; $hash{somekey} = "@array"; }

> 
> 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
> 
> ---
> 
> 
> -- 
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> 
> 
-- 
Today is Prickle-Prickle the 46th day of Discord in the YOLD 3168


Missile Address: 33:48:3.521N  84:23:34.786W


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