in reply to hash components out of order

If you'd rather not mess with tied hashes and all the magic surrounding them, you could allow them to remain stored out of order and just process them in order. Replace:
foreach $key (keys (%data)){
With:
foreach $key (sort keys (%data)){
... for example.