while () { chomp; # <== move the chomp here my ($key, $value) = split /;/; $calhash{$key} = $value; # not '= $key' } #### foreach my $key (sort {$b cmp $a} keys %calhash) { my $value = $calhash{$key}; print TESTFILE "$key;$value\n"; }