in reply to sum function
#!/usr/bin/perl -w use strict; my %hash = ( foo => '123' , bar => '234', batz => '345', bongo => '100' ); #send reference to hash, and list of keys to sum print sum_hash( \%hash, 'foo', 'bar', 'batz' ) . "\n"; #send reference to hash, will sum all numeric values print sum_hash( \%hash ) . "\n"; sub sum_hash { my $t_hash = shift; my $total; if (@_) { for (@_) {$total += $t_hash->{$_}; } } else { for ( keys %$hashref ) { $total += $t_hash->{$_}; }; } return $total; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: sum function
by chipmunk (Parson) on Dec 26, 2000 at 02:21 UTC | |
by ichimunki (Priest) on Dec 26, 2000 at 05:18 UTC |