in reply to Bulk Hash Assignment
but foo was alone. so bar was created.#!/usr/bin/perl -w use strict; my %foo = ( camshaft => 'lumpy', gearbox => 'shifty', drivetrain => 'speedy', smokestack => 'dirty', lights => 'bright eyes', );
but bar was different... and empty. foo wants to understand bar, and a map was made...my %bar; @bar{ qw/camshaft gearbox1 the_drivetrain _other/ } = '';
still, bar was empty, so along comes schlep to help...my %map_foo_to_bar = ( camshaft => 'camshaft', gearbox => 'gearbox1', drivetrain => 'the_drivetrain', smokestack => 'my_smokestack', junkyard => '_junkyard', );
so it is written, so it shall be done.sub schlep($$$) { my( $map_ref, $from_ref, $to_ref ) = ( shift, shift, shift ); for( keys %{ $from_ref } ) { $to_ref->{ $map_ref->{$_} } = $from_ref->{$_} if( exists $map_ref->{$_} && exists $to_ref->{ $map_ref->{ +$_} } ) } } schlep(\%map_foo_to_bar, \%foo, \%bar); use Data::Dumper; print Dumper \%bar;
~Particle ;Þ
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Bulk Hash Assignment
by tadman (Prior) on Apr 17, 2002 at 20:40 UTC | |
by particle (Vicar) on Apr 17, 2002 at 21:12 UTC | |
by tadman (Prior) on Apr 17, 2002 at 22:20 UTC | |
by pdcawley (Hermit) on Apr 19, 2002 at 07:31 UTC |