Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Hash element exists/delete

by tobyink (Canon)
on Jan 22, 2013 at 08:52 UTC ( [id://1014602]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1014602]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-03-19 05:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found