For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2005 > How to change the value of a Hash Key









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to change the value of a Hash Key
Mallik

2005-06-08, 3:57 am

Hi,

I have a below hash (printed using Dumper).

$VAR1 = {
'Technologies':'Optical':'Dense Wavelength Division Multiplexing
(DWDM)'
};
$VAR2 = {
'typeInfoSortKey' => '',
'mdfName' => 'Linear with Optical Add/Drop Multiplexing (OADM)',
'childKeys' => '',
'docs' => '',
'docTitles' => '',
'mdfId' => '279524369',
'level' => '4',
'parentMdfId' => '268436078',
'count' => 0,
'link' => 'Linear_with_Optical_Add_D_279524369.html'
};

Here, the key is 'Technologies':'Optical':'Dense Wavelength Division
Multiplexing (DWDM)'.

Now, I want to replace it with something like 'abc'.

Any pointers on this?

Thanks & Regards,
Mallik


Mallik

2005-06-08, 8:57 am

Hi Thomas,

Thanks for your reply.

I want to accomplish some thing like this...

%hash =3D (
"abc" =3D> "mallik",
"xyz" =3D> "arjun",
"mno" =3D> "priya"
);

Need be changed to

%hash =3D (
"123" =3D> "mallik",
"243" =3D> "arjun",
"532" =3D> "priya"
);

The key value abc is changed to 123 and so on..

Hope my requirements are clear now.

Any help in this regard is greatly appreciated.

Thanks & Regards,
Mallik

-----Original Message-----
From: Thomas B=E4tzler [mailto:t.baetzler@bringe.com]=20
Sent: Wednesday, June 08, 2005 11:50 AM
To: 'beginners@perl.org'
Cc: 'Mallik'
Subject: RE: How to change the value of a Hash Key

Mallik <mkodiche@cisco.com> asked:

> I have a below hash (printed using Dumper).

[...]
> Here, the key is 'Technologies':'Optical':'Dense Wavelength=20
> Division Multiplexing (DWDM)'.
>=20
> Now, I want to replace it with something like 'abc'.
>=20
> Any pointers on this?


Did you mean something like this?

$hash{'bar'} =3D $hash{'foo'};
delete $hash{'bar'};

HTH,
Thomas


--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
FreeFall

2005-06-08, 8:57 am

The following code may be not simple:
===code===
#!/usr/bin/perl
use warnings;
use strict;

my %hash = ("abc"=>"mallik","xyz"=>"ariun","mno"=>"priya");
my %hash2 = ("abc"=>"123","xyz"=>"243","mno"=>"532");
foreach (keys %hash2) {
$hash{$hash2{$_}} = $hash{$_};
delete $hash{$_};
}
foreach (keys %hash) {
print "$_ ==> $hash{$_}\n";
}
===code===
On Wed, 8 Jun 2005 14:02:51 +0530
"Mallik" <mkodiche@cisco.com> wrote:

> Hi Thomas,
>
> Thanks for your reply.
>
> I want to accomplish some thing like this...
>
> %hash = (
> "abc" => "mallik",
> "xyz" => "arjun",
> "mno" => "priya"
> );
>
> Need be changed to
>
> %hash = (
> "123" => "mallik",
> "243" => "arjun",
> "532" => "priya"
> );
>
> The key value abc is changed to 123 and so on..
>
> Hope my requirements are clear now.
>
> Any help in this regard is greatly appreciated.
>
> Thanks & Regards,
> Mallik
>
> -----Original Message-----
> From: Thomas B__tzler [mailto:t.baetzler@bringe.com]
> Sent: Wednesday, June 08, 2005 11:50 AM
> To: 'beginners@perl.org'
> Cc: 'Mallik'
> Subject: RE: How to change the value of a Hash Key
>
> Mallik <mkodiche@cisco.com> asked:
>
> [...]
>
> Did you mean something like this?
>
> $hash{'bar'} = $hash{'foo'};
> delete $hash{'bar'};
>
> HTH,
> Thomas
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>



--
Whatever you do will be insignificant,but
the important is you do it!

It doesn't matter who you are, it's what
you do that takes you far!
Thomas Bätzler

2005-06-08, 8:57 am

Hello Mallik,

you asked:
> I want to accomplish some thing like this...
>
> %hash = (
> "abc" => "mallik",
> "xyz" => "arjun",
> "mno" => "priya"
> );
>
> Need be changed to
>
> %hash = (
> "123" => "mallik",
> "243" => "arjun",
> "532" => "priya"
> );
>
> The key value abc is changed to 123 and so on..
>
> Hope my requirements are clear now.


If your new keys don't overlap with the old ones, you could
do it like this:

# set up a key mapping
my %mapping = ( 'abc' => '123', 'xyz' => '243', 'mno' => '532' );

my %hash = (
"abc" => "mallik",
"xyz" => "arjun",
"mno" => "priya"
);

# not recommended for large hashes, but syntactically elegant
@hash{values %mapping} = @hash{keys %mapping};
delete @hash{keys %mapping};

# slightly less attractive code with a low memory overhead
foreach my $key ( keys %mapping ){
$hash{$mapping{$key}} = $hash{$key};
delete $hash{$key};
}

If keys worked like substr, you might've gotten away with

keys %hash = @mapping{keys %hash};

Alas, it doesn't.

Doing the same operation for overlapping key sets is left as an
exercise for the reader ;-)

HTH,
Thomas

John W. Krahn

2005-06-08, 8:57 am

Mallik wrote:
> Hi,


Hello,

> I have a below hash (printed using Dumper).
>
> $VAR1 = {
> 'Technologies':'Optical':'Dense Wavelength Division Multiplexing
> (DWDM)'
> };
> $VAR2 = {
> 'typeInfoSortKey' => '',
> 'mdfName' => 'Linear with Optical Add/Drop Multiplexing (OADM)',
> 'childKeys' => '',
> 'docs' => '',
> 'docTitles' => '',
> 'mdfId' => '279524369',
> 'level' => '4',
> 'parentMdfId' => '268436078',
> 'count' => 0,
> 'link' => 'Linear_with_Optical_Add_D_279524369.html'
> };
>
> Here, the key is 'Technologies':'Optical':'Dense Wavelength Division
> Multiplexing (DWDM)'.
>
> Now, I want to replace it with something like 'abc'.
>
> Any pointers on this?


$ perl -le'
my %a = "a" .. "j";
print join " ", %a;
$a{ new } = delete $a{ e };
print join " ", %a;
'
e f c d a b g h i j
c d a b g h new f i j


And you can use that on multiple keys:

$ perl -le'
my %a = "a" .. "j";
print join " ", %a;
@a{ "M", "N", "O", "P", "Q" } = delete @a{ "a", "c", "e", "g", "i" };
print join " ", %a;
'
e f c d a b g h i j
Q j O f M b N d P h




John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com