in reply to Why is my code producing weird output?

The code works fine for me : Output:
$ perl pm1.pl < pm1-dat.txt george:2~~14 nick:5~~10~~20 peter:3
My perl : This is perl 5, version 34, subversion 0 (v5.34.0) built for x86_64-linux-gnu-thread-multi

Your code uses an un-declared $a which is also used by sort.
Your perl seems to encounter side-effects of that.
I recommend you use a declared variable NOT named $a, or $b.

                "These opinions are my own, though for a small fee they be yours too."

Replies are listed 'Best First'.
Re^2: Why is my code producing weird output?
by hv (Prior) on Aug 30, 2023 at 23:31 UTC

    Note that in the line for $a( sort keys %res ), the work for the sort is completed before the first value is assigned to $a. So while this might not be best practice, I do not think it is causing the problem in this case.