in reply to Hash element exists/delete

There's no need to for two nested loops. That will get really slow when %hash gets big.

use strict; use warnings; use utf8::all; my %hash = ( the => 5, good => 3, The => 2, Bad => 2, uGly => 1, uglY => 1, École => 1, ); for my $key (keys %hash) { next unless $key =~ /^\p{Uppercase_letter}/; my $lckey = lc $key; $hash{$lckey} += delete $hash{$key} if exists $hash{$lckey}; } for my $key (sort {$hash{$b} <=> $hash{$a}} keys %hash) { print "$key\t$hash{$key}\n"; }

Update: replaced /^[A-Z]/ match with a Unicode-aware regexp.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
}) } say Cow-

Replies are listed 'Best First'.
Re^2: Hash element exists/delete
by eyepopslikeamosquito (Archbishop) on Jan 22, 2013 at 11:40 UTC