%hash + @keys -> @value_refs; existing associations only

Discussion in 'Perl Misc' started by Ivan Shmakov, Mar 15, 2012.

  1. Ivan Shmakov

    Ivan Shmakov Guest

    Given @keys and a %hash, a list of values could be produced
    easily, like:

    my @vals
    = @hash{@keys};

    Likewise, for the list of references:

    my @refs
    = \@hash{@keys};

    I wonder, is it necessary to use map to get a list of references
    /considering the existing associations only/?

    my @refs
    = map { exists ($hash{$_}) ? \$hash{$_} : undef; } (@keys);

    I don't care about the missing keys, BTW. Thus, I can use
    something like the following instead, but it's even more
    verbose.

    my @refs = ();
    foreach my $k (@keys) {
    push (@refs, \$hash{$k})
    if (exists ($hash{$k}));
    }

    --
    FSF associate member #7257
     
    Ivan Shmakov, Mar 15, 2012
    #1
    1. Advertising

  2. On Thu, 15 Mar 2012 13:44:01 +0700, Ivan Shmakov wrote:


    > I wonder, is it necessary to use map to get a list of references
    > /considering the existing associations only/?
    >
    > my @refs
    > = map { exists ($hash{$_}) ? \$hash{$_} : undef; } (@keys);
    >
    > I don't care about the missing keys, BTW. Thus, I can use

    something
    > like the following instead, but it's even more verbose.
    >
    > my @refs = ();
    > foreach my $k (@keys) {
    > push (@refs, \$hash{$k})
    > if (exists ($hash{$k}));
    > }


    my @refs
    = map { exists ($hash{$_}) ? \$hash{$_} : () } (@keys);

    HTH,
    M4
     
    Martijn Lievaart, Mar 15, 2012
    #2
    1. Advertising

  3. Ivan Shmakov

    Dr.Ruud Guest

    On 2012-03-15 07:44, Ivan Shmakov wrote:

    > Given @keys and a %hash, a list of values could be produced
    > easily, like:
    >
    > my @vals
    > = @hash{@keys};
    >
    > Likewise, for the list of references:
    >
    > my @refs
    > = \@hash{@keys};
    >
    > I wonder, is it necessary to use map to get a list of references
    > /considering the existing associations only/?
    >
    > my @refs
    > = map { exists ($hash{$_}) ? \$hash{$_} : undef; } (@keys);
    >
    > I don't care about the missing keys, BTW. Thus, I can use
    > something like the following instead, but it's even more
    > verbose.
    >
    > my @refs = ();
    > foreach my $k (@keys) {
    > push (@refs, \$hash{$k})
    > if (exists ($hash{$k}));
    > }



    Can you sketch why you would need such a data set?
    Would aliases also be OK?

    --
    Ruud
     
    Dr.Ruud, Mar 15, 2012
    #3
  4. Ivan Shmakov

    Ivan Shmakov Guest

    >>>>> Martijn Lievaart <> writes:
    >>>>> On Thu, 15 Mar 2012 13:44:01 +0700, Ivan Shmakov wrote:


    >> I wonder, is it necessary to use map to get a list of references
    >> /considering the existing associations only/?


    >> my @refs
    >> = map { exists ($hash{$_}) ? \$hash{$_} : undef; } (@keys);


    >> I don't care about the missing keys, BTW. Thus, I can use something
    >> like the following instead, but it's even more verbose.


    [...]

    > my @refs
    > = map { exists ($hash{$_}) ? \$hash{$_} : () } (@keys);


    Indeed. Thanks!

    Now I'd combine that with List::Util::reduce to get...

    $ perl -we 'use strict;
    use Data::Dump;
    use List::Util qw (reduce);
    my $obj = {
    "a" => { "b" => "c" },
    "d" => { "e" => { "f" => "g" }, "h" => "i" },
    "j" => "k"
    };
    my @krefs
    = ([qw (a b)], [qw (d e f)], [qw (d h)], [qw (j)]);
    foreach my $kr (@krefs) {
    print (join (".", @$kr),
    " = ",
    (reduce { $a->{$b}; } ($obj, @$kr)),
    "\n");
    }'
    Name "main::b" used only once: possible typo at -e line 1.
    Name "main::a" used only once: possible typo at -e line 1.
    a.b = c
    d.e.f = g
    d.h = i
    j = k
    $

    Though I wonder whether I can use a subroutine reference instead
    of a code block or not?

    --
    FSF associate member #7257
     
    Ivan Shmakov, Mar 16, 2012
    #4
    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. Mike C. Fletcher
    Replies:
    1
    Views:
    289
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Sep 5, 2004
  2. rp
    Replies:
    1
    Views:
    543
    red floyd
    Nov 10, 2011
  3. Jean-Marc Molina
    Replies:
    1
    Views:
    217
    Jean-Marc Molina
    Jun 9, 2006
  4. Dr Nic
    Replies:
    2
    Views:
    124
    Dr Nic
    Aug 3, 2006
  5. Replies:
    14
    Views:
    188
    C.DeRykus
    Dec 22, 2009
Loading...

Share This Page