my %hash; for (qw(first second first third second fourth)) { exists $hash{$_} or $hash{$_} = keys %hash; # Put stuff in like this } print "$_\n" for sort {$hash{$a} <=> $hash{$b}} keys %hash; # Get stuff out like this