Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Slice of a hash of hashes

Reply
Thread Tools

Slice of a hash of hashes

 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      08-10-2006
Kindly consider the following code which illustrates my question:

#!/usr/bin/perl
use strict; use warnings;
use Data:umper;

my %hash;

$hash{1}{'foo'} = 'one';
$hash{2}{'foo'} = 'two';
$hash{3}{'foo'} = 'three';

my @foos;
push @foos, $hash{$_}{'foo'} for keys %hash;

print Dumper \@foos;

__END__

As you can see, I have created an array containing the values of the
various "foo" keys within the hash. But that seems a bit kludgy. It
seems I ought to be able to do something similar to this (with
slicing):

my @foos = @hash{keys %hash}{'foo'};

But I can't seem to figure out how to implement this approach.

I would appreciate some not-so-kludgy pointers. Thanks!

--
David Filmer (http://DavidFilmer.com)

 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      08-10-2006
>>>>> "u" == usenet <(E-Mail Removed)> writes:

u> $hash{1}{'foo'} = 'one';
u> $hash{2}{'foo'} = 'two';
u> $hash{3}{'foo'} = 'three';

u> push @foos, $hash{$_}{'foo'} for keys %hash;

u> As you can see, I have created an array containing the values of the
u> various "foo" keys within the hash. But that seems a bit kludgy. It
u> seems I ought to be able to do something similar to this (with
u> slicing):

u> my @foos = @hash{keys %hash}{'foo'};

u> But I can't seem to figure out how to implement this approach.

perl 5 can't slice any intermediate levels (hash or array) in a
structure. it can only slice a single ref value which will be at the
lowest level of your structure that you access. intermediate keys/index
values have to be single so they can lookup the next level down. perl6
has such midlevel slice capabilities.

but you can do better than your push code with map:

my @foos = map $_->{'foo'}, values %hash ;

if you want more levels than you can nest the maps or loop more. if you
need to do that often then wrap that code in subs to make it easier to
use.

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
 
 
 
Mumia W.
Guest
Posts: n/a
 
      08-10-2006
On 08/09/2006 09:23 PM, (E-Mail Removed) wrote:
> Kindly consider the following code which illustrates my question:
>
> #!/usr/bin/perl
> use strict; use warnings;
> use Data:umper;
>
> my %hash;
>
> $hash{1}{'foo'} = 'one';
> $hash{2}{'foo'} = 'two';
> $hash{3}{'foo'} = 'three';
>
> my @foos;
> push @foos, $hash{$_}{'foo'} for keys %hash;
>
> print Dumper \@foos;
>
> __END__
>
> As you can see, I have created an array containing the values of the
> various "foo" keys within the hash. But that seems a bit kludgy. It
> seems I ought to be able to do something similar to this (with
> slicing):
>
> my @foos = @hash{keys %hash}{'foo'};
>
> But I can't seem to figure out how to implement this approach.
>
> I would appreciate some not-so-kludgy pointers. Thanks!
>


I don't consider your original code to be kludgy, but here:

my @foos = map $_->{foo}, values %hash;

UNTESTED CODE


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
"Pseudo-hashes are deprecated" error and accessing a hash of hashes ernestm@mindspring.com Perl Misc 3 01-31-2006 04:40 AM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Hashes of hashes or just one hash ? Perl Learner Perl Misc 11 06-09-2005 06:55 AM
Performance Improvement of complex data structure (hash of hashes of hashes) Scott Gilpin Perl Misc 2 08-26-2004 01:02 AM



Advertisments