sort an hash


I have a hash structure like this

%data = (
   1 => {
      name => "Johnny",
      age => "21",
   2 => {
      name => "Barney",
      age => "35",
   3 => {
      name => "Mary",
      age => "18",
   4 => {
      name => "Chris",
      age => "21",

I want to sort it by all names and ages, but I can't do that.

Please note that there are some ages equal.

By the way, how can I delete the entire "2" SubHash?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

perldoc -q sort
delete $data{2};
you should probably implement this is as an array, they're really just hashes 'keyed' by integers (and more efficient)
@data=( { name => "Johnny", age => "21"}, etc. );
note that the indicies start at 0
to sort this data by name you could do
@data = sort {$a->{name} cmp $b->{name} } @data;
or by age
@data = sort {$a->{age} <=> $b->{age} } @data;
since hashes aren't stored in any specific order i don't think the same thing could be accomplished with one sort function on your "hash of hashes" as it can with this "array of hashes"

to remove a hash entry:
delete $data{2}
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

sorry to repeate you helver, you answered while i was typing so i didn't see it
jpcsAuthor Commented:

Thanks you all, specially adam923. You solved my problem.

you're welcome

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.