On Fri, 14 Jan 2005 12:09:03 +0530, Anish Kumar K.
<[EMAIL PROTECTED]> wrote:
> Hi All
> 
> I have a hash say. %browserType in which assume there are values...
> 
> %browserType=(
> "IE"=>2,
> "NETSCAPE"=>3,
> "FIREFOX"=>5
> );
> 
> I need to calculate one morething say percentage utilisation for each 
> browser..
> 
> ie..If IE is 2 = the percentage is calculated as (2/(2+3+5))*100
>        NETSCAPE=(3/(2+3+5))*100
>        FIREFOX=(5/(2+3+5))*100
> 
> and pass this in this single hash...I am new to this hash...I thought there 
> are two values possible, KEY and VALUE in hash
> is there a way I can add one more
> 
> "IE"=>2=>10
> "NETSCAPE=>3=>20
> 
> Please help
> 
> Anish

## calculate total
my $total = 0;

foreach keys (%hash) {
    $total += $hash{$_};
}

## create a new hash to an anonymous array
foreach keys (%hash) {
     $percentage = ($hash{$_}/$total) * 100;
    $browsertype{$_} = [$hash{$_}, $percentage];
}

## access
foreach keys (%browsertype) {
   print "Value: $_: $browsertype{$_}[0]\n";
   print "Percentage: $_: $browsertype{$_}[1]\n";
}

Untested though :)

Tor
Tor

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to