gitarwmn has asked for the wisdom of the Perl Monks concerning the following question:


Ahhh, I figured out my problem. The values in the hash were wrong. Thanks to all who responded.

How do I multipy the value of a hash with a scaler? Thanks
Here is my code. This is for an assignment I am doing for a class I'm taking. I'm supposed to Write a payroll calculator. Let the user enter email addresses and how many weeks each email address worked this pay period. After the user enters 'quit', print out a report of how much to pay each employee. Use the following chart to determine employee's pay: The problem is I can't even get what I have so far to multiply the hash times number of weeks worked. Any idea 's as to why my multiplication line is not working would be greatly helpful. Thanks
my %emails = (gwbush => "\$19.95", billc => "\$19.95", igor => "\$19.95", jdoe => "\$25.00", maryp => "\$35.00", bgates => "\$50.00", joelg => "\$1.50"); my $ename; my $weeks; print "Enter Email address.\n"; print "\nPress 'q' to quit\n"; while (){ print "\nEmail?\n"; chomp ($ename = <STDIN>); if ($ename eq 'q'){ print "Quiting program"; last; } print "Weeks worked? \n"; chomp ($weeks = <STDIN>); print "\n$ename\t$emails{$ename}\n"; $emails{$ename} *= $weeks; print "We owe $ename\t$emails{$ename}\n"; }

Replies are listed 'Best First'.
Re: multiply hash values
by fglock (Vicar) on Oct 27, 2004 at 20:55 UTC
    use strict; my %h = ( a => 10, b => 20 ); my $m = 3; $h{$_} *= $m for keys %h;
Re: multiply hash values
by borisz (Canon) on Oct 27, 2004 at 20:56 UTC
    my %h = qw/a 1 b 2 c 3/; my $something = 2; # multiply one element $h{a} *= $something; # multiply all elements for ( values %h ) { $_ *= $something; }
    Boris
Re: multiply hash values
by Fletch (Bishop) on Oct 27, 2004 at 22:56 UTC

    Fore.

    my %h = qw( a 1 b 2 c 3 ); @h{ keys %h } = map { $_ * 4 } values %h;

    Update: True. And the subsequent followup's still verbose: $_*=4for values%h; :)

      Ooops, you used too big a club ...
      my %h = qw( a 1 b 2 c 3 ); map { $_ *= 4 } values %h;
        $_ *= 4 for values %h;