Hash slice as hash

D

David

Dear all,

I'd like extract particular values from a hash, as a hash. I've done
it in the code below using a foreach loop

Is there a cleaner way?

Thanks.

sub get_db_properties
{
my $rec = shift;
my @db_properties = qw ( db_user db_pass db_server );
my $ret;

foreach my $prop ( @db_properties)
{
$ret->{$prop} = $rec->{$prop};
}

return $ret;
}
 
A

Anno Siegel

David said:
Dear all,

I'd like extract particular values from a hash, as a hash. I've done
it in the code below using a foreach loop

Is there a cleaner way?

Thanks.

sub get_db_properties
{
my $rec = shift;
my @db_properties = qw ( db_user db_pass db_server );
my $ret;

foreach my $prop ( @db_properties)
{
$ret->{$prop} = $rec->{$prop};
}

return $ret;
}

There are alternatives, though I don't think of them as definitely
"cleaner".

The "foreach" loop could be a statement modifier:

$ret{ $_} = $rec->{ $_} for @db_properties;

or it could be replaced by a map(). In full:

sub get_db_properties {
my $prop = shift;
+{ map { $_ => $prop->{ $_} } qw( db_user db_pass db_server)};
}

Anno
 
T

Tad McClellan

David said:
Dear all,

I'd like extract particular values from a hash, as a hash. I've done
it in the code below using a foreach loop

Is there a cleaner way?

Thanks.

sub get_db_properties
{
my $rec = shift;
my @db_properties = qw ( db_user db_pass db_server );
my $ret;

foreach my $prop ( @db_properties)
{
$ret->{$prop} = $rec->{$prop};
}

return $ret;
}


You could do it using actual hash slices (untested):

@{$ret}{ @db_properties } = @{$rec}{ @db_properties };
 
G

Gyruss

Tad McClellan said:
You could do it using actual hash slices (untested):

@{$ret}{ @db_properties } = @{$rec}{ @db_properties };

The following line in Learning Perl discouraged me from trying that. It
certainly looks like it should work though!

17.6.2 Hash Slice - Learning Perl

It's normal at this point to wonder why there's no percent sign ("%") here,
when we're talking about a hash. That's the marker that means there's a
whole hash; a hash slice (like any other slice) is always a list, not a hash
 
P

Paul Lalli

Gyruss said:
The following line in Learning Perl discouraged me from trying that. It
certainly looks like it should work though!

17.6.2 Hash Slice - Learning Perl

It's normal at this point to wonder why there's no percent sign ("%") here,
when we're talking about a hash. That's the marker that means there's a
whole hash; a hash slice (like any other slice) is always a list, not
a hash

Yes, but a hash slice is an l-value, meaning it can be assigned to. So
while the hash slice is not a hash, the hash for which it is a slice IS
a hash. In plainer terms, yes, it works. After Tad's code above, $ret
will be a hash reference, and the hash it references will contain the
keys and values from the original hash referred to by $rec.

Example:

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $rec = {one=>1, two=>2, three=>3};
my @nums = qw/one three/;
my $ret;
@{$ret}{@nums} = @{$rec}{@nums};
print Dumper($rec, $ret);
__END__

$VAR1 = {
'three' => 3,
'one' => 1,
'two' => 2
};
$VAR2 = {
'three' => 3,
'one' => 1
};


Paul Lalli
 

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

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top