Lyle Kopnicky wrote:

> Hi folks,
> 
> I have a seemingly simple problem, but I can't find a satisfying 
> solution.  I have a function which tests to see if a value represents 
> what I want to call "true".  Here's a simplified version:
> 
>     if ($val =~ /true/i || $val =~ /t/i || $val != 0) { return 1; } else 
> { return 0; }
> 
> The text might be numeric or not.  If it is numeric, I want to accept 
> anything but zero as true.  But, if I run this on a textual non-true 
> value, such as "false", I get:
> 
>   Argument "false" isn't numeric in numeric ne (!=) at ...
> 
> The code works, but I don't want to get the warning (I'm using 
> 'warnings').  So, how can I test to see if it's a numeric value, before 
> I try to use it as one?  I tried using int() to convert it, but that 
> gives the same warning.
> 
> Any ideas?  Thanks.

foreach (qw(0 1 t true f false), undef) {
        my $val = defined $_ ? $_ : '<undef>';
        print STDOUT is_true ($_) ? "$val => True\n" : "$val => False\n";
}

sub is_true {
        my $val = shift;
return 1 if defined $val and ($val =~ /^t/i or ($val =~ /^\d+$/ and $val != 0));
return 0;

}
__END__

0 => False
1 => True
t => True
true => True
f => False
false => False
<undef> => False


-- 
  ,-/-  __      _  _         $Bill Luebkert    Mailto:[EMAIL PROTECTED]
 (_/   /  )    // //       DBE Collectibles    Mailto:[EMAIL PROTECTED]
  / ) /--<  o // //      Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to