Slice of a hash of hashes

Discussion in 'Perl Misc' started by usenet@DavidFilmer.com, Aug 10, 2006.

  1. Guest

    Kindly consider the following code which illustrates my question:

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

    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)
    , Aug 10, 2006
    #1
    1. Advertising

  2. Uri Guttman Guest

    >>>>> "u" == usenet <> 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.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Aug 10, 2006
    #2
    1. Advertising

  3. Mumia W. Guest

    On 08/09/2006 09:23 PM, wrote:
    > Kindly consider the following code which illustrates my question:
    >
    > #!/usr/bin/perl
    > use strict; use warnings;
    > use Data::Dumper;
    >
    > 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
    Mumia W., Aug 10, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott  Gilpin
    Replies:
    2
    Views:
    209
  2. David

    Hash slice as hash

    David, Oct 25, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    85
    Paul Lalli
    Oct 25, 2004
  3. Perl Learner

    Hashes of hashes or just one hash ?

    Perl Learner, Jun 8, 2005, in forum: Perl Misc
    Replies:
    11
    Views:
    205
  4. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    199
  5. Replies:
    3
    Views:
    197
Loading...

Share This Page