Copying part of a hash to another

P

pmak00

How can I abbreviate the following code:

$foo->{apple} = $bar->{apple};
$foo->{orange} = $bar->{orange};
$foo->{pear} => $bar->{pear};
$foo->{grapefruit} = $bar->{grapefruit};

I'd like to be able to type something like:

hashCopy($foo, $bar, qw(apple orange pear grapefruit);

Is there already a built-in function like hashCopy()? Or do I have to
make my own?
 
F

Fabian Pilkowski

How can I abbreviate the following code:

$foo->{apple} = $bar->{apple};
$foo->{orange} = $bar->{orange};
$foo->{pear} => $bar->{pear};
$foo->{grapefruit} = $bar->{grapefruit};

Use a loop to do similiar things more than once, e.g.

$foo->{$_} = $bar->{$_} for qw( apple orange pear grapefruit );
I'd like to be able to type something like:

hashCopy($foo, $bar, qw(apple orange pear grapefruit);

Is there already a built-in function like hashCopy()? Or do I have to
make my own?

You could roll your own by wrapping the for loop into a sub.

sub hashCopy {
$_[0]->{$_[$_]} = $_[1]->{$_[$_]} for 2 .. $#_;
}

regards,
fabian
 
T

Tad McClellan

How can I abbreviate the following code:

$foo->{apple} = $bar->{apple};
$foo->{orange} = $bar->{orange};
$foo->{pear} => $bar->{pear};
$foo->{grapefruit} = $bar->{grapefruit};

my @keys = qw/ apple orange pear grapefruit /;
@foo{@keys} = @bar{@keys}; # a "hash slice"
 
K

Klaus Eichner

How can I abbreviate the following code:

$foo->{apple} = $bar->{apple};
$foo->{orange} = $bar->{orange};
$foo->{pear} => $bar->{pear};
$foo->{grapefruit} = $bar->{grapefruit};

I'd like to be able to type something like:

hashCopy($foo, $bar, qw(apple orange pear grapefruit);

Is there already a built-in function like hashCopy()? Or do I have to
make my own?

I don't understand your code.

Perhaps you mean this instead?

$foo{apple} = $bar{apple};
$foo{orange} = $bar{orange};
$foo{pear} = $bar{pear};
$foo{grapefruit} = $bar{grapefruit};

In this case you could use hash-slices:

=======================================
use strict;
use warnings;

my (%foo, %bar);
my @fruits = ('apple', 'orange', 'pear', 'grapefruit');

@bar{@fruits} = ('AP', 'OR', 'PE', 'GR');

print "\n";
print "\$bar{$_} = '".$bar{$_}."'\n" for sort keys %bar;

@foo{@fruits} = @bar{@fruits};

print "\n";
print "\$foo{$_} = '".$foo{$_}."'\n" for sort keys %foo;
=======================================
 
F

Fabian Pilkowski

* Tad McClellan said:
my @keys = qw/ apple orange pear grapefruit /;
@foo{@keys} = @bar{@keys}; # a "hash slice"

Since you didn't mention `perldoc perlreftut` or the like, I think you
won't let it as an exercise for the reader:

@$foo{@keys} = @$bar{@keys}; # a "hashref slice"

regards,
fabian
 
J

John W. Krahn

ZapX said:
What about

my *foo = \$bar;

think that should work...

Well for one thing, you can't use my() on a typeglob like *foo (try it and
report back your findings.) And secondly, the subject says "Copying *part* of
a hash" and your example does not copy any of the data let alone only a part
of it.


John
 

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
474,172
Messages
2,570,933
Members
47,472
Latest member
blackwatermelon

Latest Threads

Top