Gossamer Forum
Home : General : Perl Programming :

Zero a number or a letter - class 101

Quote Reply
Zero a number or a letter - class 101
Hello , I am reading data in from an Ascii comma delimited text file information

using the Text::ParseWords module, with @bob = &parse_line(",", 0, $_);

as per split the comma delimited file with function @words = &parse_line($delim, $keep, $line);

What is a simple way to make sure a value is a number and not treated as a letter.

$bob[19] = 0 or is it $bob[19] ="0" ?

So when I test like this :

if ($bob[19] > 0 ) { print "Greater than zero"; }

else { print " less than or equal to zero "; }

How can I make sure the value in $bob[19] is treated as a number and not a ascii character ?

Thanks

Kode
Quote Reply
Re: [kode] Zero a number or a letter - class 101 In reply to
Do you mean like this?

Code:
if ($bob[19] =~ /^[1-9]|[1-9][0-9]+$/) {

Last edited by:

Paul: Aug 13, 2002, 12:41 PM
Quote Reply
Re: [Paul] Zero a number or a letter - class 101 In reply to
Hello Paul

I just tried your suggestions.

Thanks it works now.

I decided to use : if (($bob[19] =~ /^\d+$/) and ($bob[19] > 0)) { do some stuff }

The straight forward test if greater than zero was letting through 30 - 40% of the zero's.

Thanks

Kode

I need a donut break, I have been looking at data files for too many hours.
Quote Reply
Re: [kode] Zero a number or a letter - class 101 In reply to
The reason I didn't use \d is because that will match 0 so by using my regex above you can also remove the "and ($bob[19] > 0)" part.

Last edited by:

Paul: Aug 13, 2002, 1:19 PM
Quote Reply
Re: [Paul] Zero a number or a letter - class 101 In reply to
Thanks Paul,

I am just looking at that it must be a digit [ or more ] and must be greater than zero.

Since the value comes from a text file, it seems to interpret the zero as a zero or a "0".

Thanks Kode.
Quote Reply
Re: [kode] Zero a number or a letter - class 101 In reply to
Doesn't my regex do that?....it seems to do what you want unless I misunderstood what you want.
Quote Reply
Re: [Paul] Zero a number or a letter - class 101 In reply to
Hello Paul,

Yes your regex does the job.

I will use :

$bob[19] = 0;


if ($bob[19] =~ /^[1-9]|[1-9][0-9]+$/) { print " inside 1 st if loop\n"; }


else { print "inside 1 st else loop\n"; }

I just saw the zero in the above regex and thought it would allow zero's to pass into the i st if loop.

Sorry.

Thanks

Kode
Quote Reply
Re: [kode] Zero a number or a letter - class 101 In reply to
Basically the regex is allowing anything greater than one. I had to split it into two parts because numbers greater than 9 can contain zero's but 1-9 obviously can't
Quote Reply
Re: [Paul] Zero a number or a letter - class 101 In reply to
Thanks Paul,

Now the zero makes sence. I had not looked at it with regex eyes.

I will have to send you a beer token.

Thanks

Kode