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


I

Ivan Shmakov

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}));
}
 
Ad

Advertisements

M

Martijn Lievaart

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
 
D

Dr.Ruud

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?
 
Ad

Advertisements

I

Ivan Shmakov

Martijn Lievaart said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top