# Swap Hash Values!

Page 1 of 1

## 3 Replies - 7166 Views - Last Post: 18 March 2008 - 01:26 PM

### #1 manishsaxena2007

Reputation: 0
• Posts: 1
• Joined: 15-March 08

# Swap Hash Values!

Posted 16 March 2008 - 04:09 AM

How to swap the key,value pairs.in any Associative Array?
For Example

%mani = qw{ one-->22 ,two -->44 , three->33 ,four-> 55 }

it should be
%mani = qw{ 22-> one , 44->two, 33-> three ,55->four }

kindly write a program for that.
Is This A Good Question/Topic? 0

## Replies To: Swap Hash Values!

• D.I.C Regular

Reputation: 27
• Posts: 401
• Joined: 23-January 07

## Re: Swap Hash Values!

Posted 16 March 2008 - 11:29 AM

Post the code you have written to try and work out this extremely simple problem.

### #3 rajiv07

Reputation: 0
• Posts: 1
• Joined: 23-February 08

## Re: Swap Hash Values!

Posted 18 March 2008 - 01:21 AM

my %hash=('1'=>'2','4'=>'5');
my %hash2;

while ((\$key, \$value) = each %hash) {
\$hash2{\$value}=\$key;
}

%hash=%hash2;

foreach \$key(sort keys %hash) {
print "\$key:=>\$hash{\$key}\n";
}

I dont know this is best way to do swap.I hope it will help you.

Regards
Rajiv

• D.I.C Regular

Reputation: 27
• Posts: 401
• Joined: 23-January 07

## Re: Swap Hash Values!

Posted 18 March 2008 - 01:26 PM

Thats it, just post the answer to their school/class work for them. My goodness Rajiv, you should know better than that, you look like a smart guy.