Ling F. Zhang wrote:
> okay...I am glad @ how easy it is to use perl scalar
> both as numerical value and string...but sometimes I
> just need to do integer division the C way:
>
> when 10/3, I want it to equal to 3, not 3.33333....
> is there an operator for such operation? or do I have
> to do like:
> $a=10
> $b=3
> $c=$a/$b; # c=3.33333....
> $d=$c-$a%$b/$b; # d =3
>
> or a one-liner:
> $d = ($a - $a % $b)/%b;

Hi Ling.

What you want is 'use integer' like this

  use strict;
  use warnings;

  use integer;

  my $a = 10;
  my $b = 3;
  my $c = $a / $b;

  print map "$_\n", $a, $b, $c;

OUTPUT

  10
  3
  3

You can use it in this way if all of your program works in
integer arithmetic, but if you just want a few lines executing
this way then its scope can be bounded by a block, like this

  my $a = 10;
  my $b = 3;
  my $c = do {
    use integer;
    $a / $b;
  };


Note that it /doesn't/ change the type of the variables - they
are always just scalars - but only the way the arithmetic
operators work, so you can still assign floating point values.
Take a look at this

  use integer;

  my $x = 3.2;
  my $y = 3.6;

  printf "%f equals %f :)\n", $x, $y if $x == $y;

OUTPUT

  3.200000 equals 3.600000 :)



HTH,

Rob






-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to