>>>>> "PK" == Parag Kalra <paragka...@gmail.com> 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  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to