in reply to Sorting the hash

Perhaps this changes the problem statement a bit, and it takes us into some more advanced topics, but 'hcon' => "@arr" makes a string out of @ar. A hash key can only have a single value, in this case a string is used. But it is possible to put a reference to an array in the hash as that single value instead of converting the array to a string.

The syntax for this can be a bit daunting. If you work often with structures like this you might want to start by reading perlreftut.

Below, 'hcon' => [@ar] the square brackets say: allocate some new memory for an array and copy @ar into that memory. Then assign a reference to that new memory to the key, 'hcon'.

De-referencing the array looks like: @{$x{'hcon'}}. By preserving the array nature of @ar, the sort is a lot easier, actually just one line as shown below.

I think I got too complex here, but I thought you might want to see an example of what is possible as your Perl journey continues.

#!/usr/bin/perl -w use strict; use Data::Dumper; my @ar = ("Z","B","A"); my %x = ('period' => "2010/05", 'type' => "voice", 'tcon' => "A,B" , 'hcon' => [@ar] ); print Dumper \%x; @{$x{'hcon'}} = sort @{$x{'hcon'}}; print Dumper \%x;