Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Finding index of given value in an array

Expand Messages
  • Jonathan Paton
    ... Perl stores variables internally as one of several forms. There is little distinction between 35.0 and 35. Try: my $a = 35.0; print $a For the above the
    Message 1 of 6 , Mar 29, 2005
    • 0 Attachment
      > Offer> But I would change the "==" to "eq", so that the solution would also
      > Offer> work for arrays with non-numeric data (e.g. "my @arr = qw/a b c d
      > Offer> e/").
      >
      > No, you should change it only when appropriate. "35.0" is not eq to "35",
      > and yet they are numerically equal.

      Perl stores variables internally as one of several forms.

      There is little distinction between 35.0 and 35. Try:

      my $a = 35.0;
      print $a

      For the above the trailing zeros are not displayed, thats
      because perl either stored 35.0 as either integer 35 or
      floating point (displayed without trailing zeros).

      BUT: If you put quotes around the number, like:

      my $a = "35.0";
      print $a

      You get "35.0" because perl stored the number as string.

      Perl converts whatever it has stored internally to what the
      operation you are using requires. Perl will convert a number
      stored as a number to a string for string operations, and a
      number stored as a string to a number for number operations.

      == is a numerical comparison, so each side is converted to a
      number, if possible.

      eq is a string comparison, so each side is converted to a string,
      which is almost always possible.

      Now, if you do:

      35 == 35.0

      then that is true, because that compares two floating point numbers,
      which are equal. If you used eq instead:

      35 eq 35.0

      then that is also true, as perl converts both sides to "35" before
      comparison. However, quote the RHS:

      35 eq "35.0"

      and it will fail. "eq" doesn't give extra for free, as Offer suggested.
      Instead gives you get a subtle bug which is dependent on where
      your data came from.

      Since we are discussing floating point... you should not test for
      equality when dealing with floating point numbers. You should
      test that numbers are within a certain range. This is because

      35.0 eq 35.0000000000001

      is true, or false, depending on the precision used to store the
      numbers!

      Hopefully ALL of the above is correct... if not it's a typo... honest ;-)
      Check everything for yourself, except Randal who is correct on this.

      Jonathan Paton

      --
      #!perl
      $J=' 'x25 ;for (qq< 1+10 9+14 5-10 50-9 7+13 2-18 6+13
      17+6 02+1 2-10 00+4 00+8 3-13 3+12 01-5 2-10 01+1 03+4
      00+4 00+8 1-21 01+1 00+5 01-7 >=~/ \S\S \S\S /gx) {m/(
      \d+) (.+) /x,, vec$ J,$p +=$2 ,8,= $c+= +$1} warn $J,,
    Your message has been successfully submitted and would be delivered to recipients shortly.