in reply to Convert arithmetic expression from Excel to Perl syntax

^ doesn't mean "to the power of" in Perl, it means "exclusively ored with". Change $temp^2 to $temp*$temp and $vol^2 to $vol*$vol.

Update: As other pointed out, Perl does have a power operator: ** (although it's not quite as fast, as you can see in Benchmarks I posted below).

Replies are listed 'Best First'.
Re^2: Convert arithmetic expression from Excel to Perl syntax
by throttle (Beadle) on Nov 04, 2004 at 18:03 UTC
    You can also di $temp**2 or $vol**2. Exactly the same, but faster.
      You're right that ** works too, but you're wrong about it being faster (at least on my system).
      # srand(0) is used to make sure both # subs are using the same input set. sub m1 { unless ($i1) { srand(0); $i1=1; } $n = rand; $p = $n ** 2; } sub m2 { unless ($i2) { srand(0); $i2=1; } $n = rand; $p = $n * $n; } use Benchmark; Benchmark::cmpthese(0, { '$n**2' => \&m1, '$n*$n' => \&m2, }); __END__ Rate $n**2 $n*$n $n**2 722292/s -- -29% $n*$n 1013884/s 40% --
        when i said it was faster i meant that 'writing it' is faster