my %param; foreach ( sort { $user_details{$a} cmp $user_details{$b} } keys %user_details ){ #this next line will clobber $param{user_name} each time through. #$param{user_name} = $user_details{$_}; #do your stuff. print "the key is $_\n"; print "the value is $user_details{$_}\n"; }