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

testing 3-way equality

Expand Messages
  • Greg Matheson
    How do I test that 3 variables are equal? I tried: DB x { 1==1} 0 1 DB x {1==1==1} syntax error at (eval 9)[C:/perl/5.8.2/lib/perl5db.pl:618] line 2,
    Message 1 of 5 , Apr 4, 2004
    • 0 Attachment
      How do I test that 3 variables are equal?

      I tried:

      DB<4> x { 1==1}
      0 1
      DB<5> x {1==1==1}
      syntax error at (eval 9)[C:/perl/5.8.2/lib/perl5db.pl:618] line 2, near "1=="

      Do I have to do:

      $x == $y and $y == $z

      DB<7> x { ( 1 == 1 ) and ( 1 == 1 ) }
      0 1
      DB<8> x { 1 == 1 and 1 == 1 }
      0 1

      --
      Greg Matheson, Taiwan
    • Erik Tank
      $x == $y and $y == $z There is no (at least I am not aware of) way of testing an infinite amount of variable against each other so you have to take them in
      Message 2 of 5 , Apr 4, 2004
      • 0 Attachment
        $x == $y and $y == $z

        There is no (at least I am not aware of) way of testing an infinite
        amount of variable against each other so you have to take them in pairs.

        Erik

        This fight for my life isn't getting behind me
        ...
        Just take me away from this hell I've created
        And I'm afraid
        I'm breaking my own vows knowing I'll go down in flames
        - Godsmack (Make Me Believe)



        Greg Matheson wrote:
        > How do I test that 3 variables are equal?
        >
        > I tried:
        >
        > DB<4> x { 1==1}
        > 0 1
        > DB<5> x {1==1==1}
        > syntax error at (eval 9)[C:/perl/5.8.2/lib/perl5db.pl:618] line 2, near "1=="
        >
        > Do I have to do:
        >
        > $x == $y and $y == $z
        >
        > DB<7> x { ( 1 == 1 ) and ( 1 == 1 ) }
        > 0 1
        > DB<8> x { 1 == 1 and 1 == 1 }
        > 0 1
        >
      • Greg Matheson
        ... I should have experimented with: main::(-e:1): 0 DB x { 2 ==2 and 2 == 2 } 0 1 to make sure what was being returned was the boolean 1, rather than
        Message 3 of 5 , Apr 4, 2004
        • 0 Attachment
          On Sun, 04 Apr 2004, Erik Tank wrote:

          > $x == $y and $y == $z
          >
          > There is no (at least I am not aware of) way of testing an infinite
          > amount of variable against each other so you have to take them in pairs.

          > Greg Matheson wrote:
          > > How do I test that 3 variables are equal?

          > > DB<8> x { 1 == 1 and 1 == 1 }

          I should have experimented with:

          main::(-e:1): 0
          DB<1> x { 2 ==2 and 2 == 2 }
          0 1

          to make sure what was being returned was the boolean 1, rather
          than the value 1.

          I have a whole lot of these 3-way tests to run, so I think
          I will set up a hash like this

          $hash{ $x }++
          $hash{ $y }++
          $hash{ $z }++

          and see if grep( /^3$/, values @hash ) or grep( { $hash{$_} == 3 } keys @hash )
          or something.

          --
          Greg Matheson, tAiwan
        • Charles K. Clarkson
          ... A hash works best for comparing strings. This sub works on any amount of strings, but returns a false value for equal( 00 , 0 , 0 ). The warning allows
          Message 4 of 5 , Apr 5, 2004
          • 0 Attachment
            Greg Matheson <lang@...> wrote:
            :
            : to make sure what was being returned was the boolean 1,
            : rather than the value 1.
            :
            : I have a whole lot of these 3-way tests to run, so I think
            : I will set up a hash like this
            :
            : $hash{ $x }++
            : $hash{ $y }++
            : $hash{ $z }++
            :
            : and see if grep( /^3$/, values @hash ) or grep( { $hash{$_}
            : == 3 } keys @hash ) or something.


            A hash works best for comparing strings. This sub works
            on any amount of strings, but returns a false value for
            equal( '00', 0 , 0 ). The warning allows values to be
            undefined.

            sub equal {
            my %hash;
            no warnings 'uninitialized';
            @hash{ @_ } = ();
            return keys %hash == 1;
            }


            For numerical equality of three values this works:

            sub numerically_equal {
            return $_[0] == $_[1] and $_[1] == $_[3];
            }

            Or more mathmatically:

            sub numerically_equal {
            return $_[0] + $_[1] + $_[2] == $_[0] * 3;
            }

            The above sub leads us to this more robust sub
            which tests equality on any length list or array
            except "()".

            use List::Util 'sum';

            .
            .
            .

            sub numerically_equal {
            return $_[0] * @_ == sum @_;
            }


            HTH,

            Charles K. Clarkson
            --
            Mobile Homes Specialist
            254 968-8328
          • Greg Matheson
            ... main::equal(try.pl:9): @hash{ @_ } = (); auto(-1) DB x %hash empty array DB main::equal(try.pl:10): return keys %hash == 1; auto(-1)
            Message 5 of 5 , Apr 5, 2004
            • 0 Attachment
              On Mon, 05 Apr 2004, Charles K. Clarkson wrote:

              > sub equal {
              > my %hash;
              > no warnings 'uninitialized';
              > @hash{ @_ } = ();
              > return keys %hash == 1;

              main::equal(try.pl:9): @hash{ @_ } = ();
              auto(-1) DB<14> x %hash
              empty array
              DB<15>
              main::equal(try.pl:10): return keys %hash == 1;
              auto(-1) DB<15> x %hash
              0 3
              1 undef

              So it is initializing a slice of the hash for all the arguments
              passed to it. But as the arguments are all equal, only one
              key is generated.

              DB<17> x keys %hash
              0 3

              >
              > sub numerically_equal {
              > return $_[0] == $_[1] and $_[1] == $_[3];
              > }
              >
              > Or more mathmatically:
              >
              > sub numerically_equal {
              > return $_[0] + $_[1] + $_[2] == $_[0] * 3;
              > }

              That would be a problem if $_[0] and $_[1] weren't equal, but the sum
              was still 3 times $_[0]

              DB<9> x numerically_equal(2,3,1)
              0 1

              > The above sub leads us to this more robust sub
              > which tests equality on any length list or array
              > except "()".
              >
              > use List::Util 'sum';
              >
              > .
              > .
              > .
              >
              > sub numerically_equal {
              > return $_[0] * @_ == sum @_;
              > }

              This has the same problem, I think.

              @_ in scalar context is the number of elements in the list.

              main::numerically_equal(try.pl:7):
              7: return $_[0] + $_[1] + $_[2] == $_[0] * 3;
              DB<11> x @_
              0 00
              1 0
              2 0
              DB<12> x scalar @_
              0 3

              --
              Greg Matheson, Taiwan
            Your message has been successfully submitted and would be delivered to recipients shortly.