943,994 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 7128
  • Perl RSS
Apr 28th, 2005
0

Matching negative numbers

Expand Post »
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:
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
tat2dlady is offline Offline
32 posts
since Jan 2005
Apr 29th, 2005
0

Re: Matching negative numbers

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:

Perl Syntax (Toggle Plain Text)
  1. # /* Assuming $_ is the line we are checking */
  2. if (/INT -/) {
  3. ($cmd, $integer) = split(/ /, $_);
  4. $newdata = "$cmd \-$integer";
  5. } else {
  6. $newdata = $_;
  7. }

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)
  1. while (<FH>) {
  2. print $_;
  3. }

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.
Attached Files
File Type: txt tryme.pl.txt (159 Bytes, 26 views)
File Type: txt testfile.txt (98 Bytes, 16 views)
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: Making hashes on the fly with name dictated by a variable string
Next Thread in Perl Forum Timeline: Print 5 records per table row (see script)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC