Jesse Farrell wrote:
Is there a way to have a variable recognized as a string even if it
contains a valid integer? I have a script that is taking strings from a
file that can be numbers, including zero. I have an if statement to
simply see if the variable exists (otherwise I need to throw an error),
and if the variable contains 0, the if statement is failing. Right now,
my code is essentially:
if ($variable) {
# act on $variable
} else {
# throw error
}
Is there an easy way to get the if statement to recognize a 0, or do I
need to add a special case? I don't care to keep the 0 as an integer,
if I can change it to a string somehow, that would work great.
In Perl a scalar with a numerical value is *both* a string and a number. It
looks like you want something like:
if ( length $variable ) {
# act on $variable
}
Or depending on how you made the assignment to $variable:
if ( defined $variable ) {
# act on $variable
}
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/