- 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 - $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

> - On Sun, 04 Apr 2004, Erik Tank wrote:

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

I should have experimented with:

>

> 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 }

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 - 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 - On Mon, 05 Apr 2004, Charles K. Clarkson wrote:

> sub equal {

main::equal(try.pl:9): @hash{ @_ } = ();

> my %hash;

> no warnings 'uninitialized';

> @hash{ @_ } = ();

> return keys %hash == 1;

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

>

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

> sub numerically_equal {

> return $_[0] == $_[1] and $_[1] == $_[3];

> }

>

> Or more mathmatically:

>

> sub numerically_equal {

> return $_[0] + $_[1] + $_[2] == $_[0] * 3;

> }

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

This has the same problem, I think.

> which tests equality on any length list or array

> except "()".

>

> use List::Util 'sum';

>

> .

> .

> .

>

> sub numerically_equal {

> return $_[0] * @_ == sum @_;

> }

@_ 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