R
Robert Manea
Hello,
I'm searching for different code snippets (Golfers welcome) to solve the
following problem:
#v+
%category = {
'Mozilla' => 20,
'Forte Agent' => 12,
'slrn' => 45
};
#v-
Find the longest key (most characters) and return
'$longest_key_length + 1;'.
Since the hash can get pretty big, efficiency should be an issue.
Currently I'm using this:
#v+
my @arr; for ( keys %category ) {$arr[length $_] = defined;};
my $longest_key_length = $#arr+1;
#v-
in favor over the more golfish one liner:
#v+
my $longest_key_length =
1 + length +(sort { length $b <=> length $a } keys %category)[0];
#v-
Any other suggestions are highly appreciated.
Thx & Greets, Rob
I'm searching for different code snippets (Golfers welcome) to solve the
following problem:
#v+
%category = {
'Mozilla' => 20,
'Forte Agent' => 12,
'slrn' => 45
};
#v-
Find the longest key (most characters) and return
'$longest_key_length + 1;'.
Since the hash can get pretty big, efficiency should be an issue.
Currently I'm using this:
#v+
my @arr; for ( keys %category ) {$arr[length $_] = defined;};
my $longest_key_length = $#arr+1;
#v-
in favor over the more golfish one liner:
#v+
my $longest_key_length =
1 + length +(sort { length $b <=> length $a } keys %category)[0];
#v-
Any other suggestions are highly appreciated.
Thx & Greets, Rob