>>>>> "PK" == Parag Kalra <[email protected]> writes:
PK> my @array = (1,2,3,4,2,3,1,2,1,1,1,4,6,7);
PK> my %hash;
PK> foreach my $item (@array){
PK> if (exists $hash{$item}) {
PK> $hash{$item} = $hash{$item} + 1;
PK> } else {
PK> $hash{$item} = 1;
PK> }
PK> }
this replaces the entire if/else statement:
$hash{$item}++ ;
perl will automatically make undef into 0 without warnings when using
++. there is no need to initialize it the first time. and the rest of
the time it will just increment it. it is a standard perl idiom you need
to know so you don't waste code like that.
uri
--
Uri Guttman ------ [email protected] -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/