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:

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:

# /* 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:

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. ;)

Attachments
SUB -
INT -1
JUNK LINE
SOME KIND OF LINE
INT -3
INT -5
INT 7
INT 8
INT 9
INT -1
INT -2
#!/usr/bin/perl

open(FH, "testfile");
open(RH, ">outputfile");
        while (<FH>) {
                print RH "$_";
        }
close(FH);
close(RH);
This article has been dead for over six months. Start a new discussion instead.