Acceptible idiom ?

K

Koos Pol

Although you need to actively think Perl,

!!$a

is a lot shorter than

defined $a && ($a ne "" || $a <>0 );

Comments?
 
A

Anno Siegel

Koos Pol said:
Although you need to actively think Perl,

!!$a

is a lot shorter than

defined $a && ($a ne "" || $a <>0 );

Comments?

That's a quite common idiom for normalizing booleans, not only in Perl.
It is useful in all languages that take a similar approach to booleans:
Everything is true except for a (small) number of exceptions that are
false.

I have called the construct "Das doppelte Notchen", but it takes a
native German speaker to estimate the full inanity of that pun.

Anno
 
K

Koos Pol

Anno Siegel wrote (Monday 11 October 2004 11:44):
That's a quite common idiom for normalizing booleans, not only
in Perl.

Stupid things is, I've been doing the lower for years without
realizing the upper is a lot cleaner. And just this very moment
it suddenly dawned...
 
R

Richard Gration

Although you need to actively think Perl,
!!$a
is a lot shorter than

defined $a && ($a ne "" || $a <>0 );
Comments?

It seems to me that

($a <> 0) implies ($a ne "") for all $a

so that means the test could have been written

defined $a && ($a ne "")

Am I missing something?

Rich
 
A

Anno Siegel

Richard Gration said:
It seems to me that

($a <> 0) implies ($a ne "") for all $a
^^
That's not Perl (nor was it in earlier postings) :)
so that means the test could have been written

defined $a && ($a ne "")

Am I missing something?

The possibility that someone said "$a = 0"?

But anyway, your logic escapes me. Even if it were true that
'$a != 0 implies $a ne ""' (which it isn't), it would be the
test for "" that could be discarded, not the one for 0.

Anno
 
P

Paul Lalli

Koos Pol said:
Although you need to actively think Perl, ^^^^^^^^^^

defined $a && ($a ne "" || $a <>0 );
^^^^^^

I find this contrast amusing.

Paul Lalli
 
K

Koos Pol

Richard Gration wrote (Monday 11 October 2004 13:54):
It seems to me that

($a <> 0) implies ($a ne "") for all $a

so that means the test could have been written

defined $a && ($a ne "")

Am I missing something?


Yes, you do (not your fault). I'm maintaing a script which misuses
a var to have both numeric and string values :-(
 
P

Paul Lalli

But anyway, your logic escapes me. Even if it were true that
'$a != 0 implies $a ne ""' (which it isn't),

It isn't? Can you give me a non-zero value which is string-equal to the
empty string?

The converse, $a ne '' implying $a != 0, is clearly false, as is
exemplified by the value $a = 'foo'; The original does indeed hold,
however. There are no values which are not numerically equal to 0 which
are string equal to the empty string.

.... unless I'm really not thinking well today. Feel free to show me how
I'm wrong...

Paul Lalli
 
A

Anno Siegel

Paul Lalli said:
It isn't? Can you give me a non-zero value which is string-equal to the
empty string?

use Scalar::Util qw( dualvar);
my $paradox = dualvar( 123, '');
print $paradox + 0, " is also '$paradox'\n"

But I'll admit that I wasn't thinking of this possibility, I was just
confused. For "normal" Perl variables your implication is true. That
includes "natural duals" like booleans and (on my system) also $!.

It is another consideration that you must switch off warnings to
make undisturbed use of the implication.

Anno
 
K

Koos Pol

Paul Lalli wrote (Monday 11 October 2004 14:33):
^^^^^^

I find this contrast amusing.


Yes, well, sometimes I do have my head screwed the other way
around :-D
 
R

Richard Gration

^^
That's not Perl (nor was it in earlier postings) :)
The possibility that someone said "$a = 0"? But anyway, your logic
escapes me. Even if it were true that '$a != 0 implies $a ne ""' (which
it isn't), it would be the test for "" that could be discarded, not the
one for 0. Anno

I see your point about flawed logic, however ... what's going on here.
The two expressions appear to be identical, truthwise. Now I'm confused

[rich@richg perl]$ cat truth.pl
#!/usr/bin/perl

truth('a',$c);
$c = 0;
truth('b',$c);
$c = '';
truth('c',$c);
$c = 10;
truth('d',$c);
$c = 'asdf';
truth('e',$c);

sub truth {
($t,$n) = @_;

print "$t 1" if (defined $n && ($n ne "" || $n != 0));
print "\t";
print "$t 2" if (defined $n && ($n ne ""));
print "\n";
}
[rich@richg perl]$ ./truth.pl

b 1 b 2

d 1 d 2
e 1 e 2
[rich@richg perl]$
 
S

Scott W Gifford

Paul Lalli said:
It isn't? Can you give me a non-zero value which is string-equal to the
empty string?

The problem is warnings. Arithmetically comparing a non-numeric
string, including the empty string, to a numeric value, including 0,
results in a warning under -w:

perl -w -le'print (("" != 0) ? "notzero" : "iszero");'

----Scott.
 
A

Anno Siegel

Richard Gration said:
I see your point about flawed logic, however ... what's going on here.
The two expressions appear to be identical, truthwise. Now I'm confused

*I* was confused (as has been cleared up elsewhere in the thread).
Normally Perl doesn't allow you to create a non-zero variable whose
string value is ''.

But despite appearances, the string value and the numeric value of a
scalar are completely independent. $! is a variable that shows this
behavior. dualvar() in Scalar::Util can create such variables ad lib.
Another possibility is an object whose string value and numeric value
could, again, be overloaded with anything.

Anno
 
J

Joe Smith

Richard said:
I see your point about flawed logic, however ... what's going on here.
The two expressions appear to be identical, truthwise. Now I'm confused

[rich@richg perl]$ cat truth.pl
#!/usr/bin/perl

truth('a',$c);
$c = 0;
truth('b',$c);
$c = '';
truth('c',$c);
$c = 10;
truth('d',$c);
$c = 'asdf';
truth('e',$c);

sub truth {
($t,$n) = @_;

print "$t 1" if (defined $n && ($n ne "" || $n != 0));
print "\t";
print "$t 2" if (defined $n && ($n ne ""));
print "\n";
}

That should be
print "$t 1" if (defined $n && ($n ne "" && $n != 0));

-Joe
 
E

Eric J. Roode

Although you need to actively think Perl,

!!$a

is a lot shorter than

defined $a && ($a ne "" || $a <>0 );

Comments?

First and foremost, the above is a syntax error. But let's suppose you
really meant "$a != 0" up there.

If $a is 0, then !!$a is '', but defined $a && ($a ne "" || $a != 0) is 1.
So !!$a may be shorter, but it is not equivalent.

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
K

Koos Pol

Randal L. Schwartz wrote (Monday 11 October 2004 16:25):
Koos: replacing (defined $a && ($a != "")) with (!!$a)
There is no promise that !! returns a value of "1" for true.
Caveat executor.

That alright. Just interested in true/false and getting rid of
that ubiquitous dreadful test.

Koos
 
K

Koos Pol

Abigail wrote (Monday 11 October 2004 23:06):
\\ !!$a
\\
\\ is a lot shorter than
\\
\\ defined $a && ($a ne "" || $a <>0 );
Yes, but what is your point?

defined $a && ($a ne "" || $a <>0 );

is a syntax error,

Yes. I was typing faster then thinking. My bad.
and if you mean '!=' where you write '<>',
the expressions are not equivalent. '($a ne "" || $a != 0)' is
only false if $a is some overloaded object that is the empty
string in string context, and 0 is numeric context. So, assuming
that $a isn't such an object...

Assume I'm maintainig a script which misuses a var to have both
string and numeric values... :-(
But 'defined $a' is true for '$a == 0', but for '$a == 0',
'!!$a' is false.

And that is exactly what I was after. !! makes is possible to
check for true even with undefined variables.

Koos
 
A

Ala Qumsieh

Koos said:
And that is exactly what I was after. !! makes is possible to
check for true even with undefined variables.

Undefined variables can never be true. Your code simply reduces to:

if ($a) { ... }

but, I have a feeling you already knew that :)

--Ala
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top