Matching negative numbers

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2005
Posts: 31
Reputation: tat2dlady is an unknown quantity at this point 
Solved Threads: 0
tat2dlady tat2dlady is offline Offline
Light Poster

Matching negative numbers

 
0
  #1
Apr 28th, 2005
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:
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Matching negative numbers

 
0
  #2
Apr 29th, 2005
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:

  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:

  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, 10 views)
File Type: txt testfile.txt (98 Bytes, 2 views)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Perl
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC