[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Re: Pushing Arrays into Hashes



>I'm playing with the example on page 273 of _Programming Perl_ and
>was wondering how to push additional arrays into the appropriate
>place within the hash.
>

I think I've answered my own question, or at least worked out what
was wrong with my initial question: I needed to push a hash into an
array within a hash, which is accomplished by taking a reference to
the hash and pushing that:
--
#!/usr/bin/perl -w

use strict;

my (%hash, $item, %new_hash, );

%hash = (
	key => {
		inner_key => 'inner_value',
		array => [
			{ array_key => 'array_value1', } ,
			{ array_key => 'array_value2', },
		],
	},
);

%new_hash = ( array_key => 'array_value3', );

push @{ $hash{'key'}{array} }, \%new_hash;

for $item (@{ $hash{'key'}{array} } ) {
	print "$item->{array_key}\n";
}
--

Sorry for the noise.
-- 
My brain hurts!
SeanC
                      Mediatek Training Institute
            129 Moore Road, Berea, Durban, South Africa
    phone: +27 (0)31 305 4200              SeanC@mediatek.co.za
      fax: +27 (0)31 305 4236
                   <http://members.xoom.com/s_carte>

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org