| | |
Matching negative numbers
Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2005
Posts: 31
Reputation:
Solved Threads: 0
Does anyone know how to match a negative number? What I am doing is printing the lexemes with their token to an output file. But, the problem is negative numbers. It should print like this:
INT -1
Instead, it prints like this:
SUB -
INT 1
The SUB - is technically right because the minus sign is printed this way but I want it to recognize the -1 as an INT. Any suggestions? Thank a lot.
TIRED OF PULLING MY HAIR OUT OVER SIMPLE THINGS! :mad:
INT -1
Instead, it prints like this:
SUB -
INT 1
The SUB - is technically right because the minus sign is printed this way but I want it to recognize the -1 as an INT. Any suggestions? Thank a lot.
TIRED OF PULLING MY HAIR OUT OVER SIMPLE THINGS! :mad:
you could do a regular expression on it, or a substitution on it. I'm a bit confused about the SUB -, though, because I'm not sure if the - that comes after SUB is SUPPOSED to belong to the 1 or not. Is SUB - and INT -1 2 different "-"s, or is it moving the - from INT up to SUB? When you read the data in from the file, test it (match test) for INT - like:
That would set $newdata to contain the correct INT line, either with a - if needed, or without if not. Something to try, is to have the perl program print to stdout, the file input line by line, just to make sure that what you are reading in, is what your program is getting. So, something like:
This way you know for sure that you Perl file is getting the proper data, then you can add the - if necessary if the file is recieving everything correctly.
Perl Syntax (Toggle Plain Text)
# /* Assuming $_ is the line we are checking */ if (/INT -/) { ($cmd, $integer) = split(/ /, $_); $newdata = "$cmd \-$integer"; } else { $newdata = $_; }
That would set $newdata to contain the correct INT line, either with a - if needed, or without if not. Something to try, is to have the perl program print to stdout, the file input line by line, just to make sure that what you are reading in, is what your program is getting. So, something like:
Perl Syntax (Toggle Plain Text)
while (<FH>) { print $_; }
This way you know for sure that you Perl file is getting the proper data, then you can add the - if necessary if the file is recieving everything correctly.
![]() |
Similar Threads
- Sorting and Searching to get rid of Duplicate Numbers (C++)
- Negative Numbers Showing Up (C++)
- Counter changing to a negative number (C++)
- Positive and negative numbers (C++)
- Counting characters and dispalying numbers (C++)
Other Threads in the Perl Forum
- Previous Thread: Making hashes on the fly with name dictated by a variable string
- Next Thread: Print 5 records per table row (see script)
| Thread Tools | Search this Thread |
Tag cloud for Perl






