in reply to Sorting integer value hash keys

```#!/usr/bin/perl

my %statistics = ( 1, 0, 2, 0, 3, 0, 4, 0, 5, 0,
6, 0, 7, 0, 8, 0, 9, 0, 10, 0
);

foreach my \$statistic (sort {\$a <=> \$b} keys %statistics)
{

print \$statistic ." ".\$statistics{\$statistic}. "\n";
}
Output:
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0

Replies are listed 'Best First'.
Re^2: Sorting integer value hash keys
by JadeNB (Chaplain) on Dec 25, 2009 at 18:53 UTC
Sorry, I can't resist:
```my %statistics;
undef @statistics{ 1..10 }; # or @statistics{ 1..10 } = (0) x 10
say join "\n", sort { \$a <=> \$b } keys %statistics;
or, if you don't need a name and like the 0 values:
```say join "\n", sort { \$a <=> \$b } keys %{{ map { \$_ => 0 } 1..10 }}
You're missing a newline on the last line.
```say join "\n", sort { \$a <=> \$b } keys %{{ map { \$_ => 0 } 1..10 }}
should becan be shortened to
```say for sort { \$a <=> \$b } keys %{{ map { \$_ => 0 } 1..10 }}
Furthermore, since you assume the list is constant, that can be simplified to
```say for sort { \$a <=> \$b } 1..10;
```say for sort { \$a <=> \$b } map "\$_", 1..10;
```say for sort { \$a <=> \$b } 1..10;