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

#1 manishsaxena2007  Icon User is offline

  • New D.I.C Head

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

Swap Hash Values!

Post icon  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!

#2 KevinADC  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 rajiv07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 KevinADC  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1