From: "Jonathan M. Katz" <jmkatz-72@worldnet.att.net>
Newsgroups: comp.sys.hp48
Subject: Re: Numbers from string? (Long)
Date: 15 Oct 1998 06:17:26 GMT
Organization: .
Lines: 186
Message-ID: <3625936A.3A35@worldnet.att.net>
References: <702dkh$756$1@nnrp1.dejanews.com>
NNTP-Posting-Host: 12.64.130.51
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Mailer: Mozilla 3.0C-WorldNet  (Win95; U)
Xref: republic.btigate.com comp.sys.hp48:17325

noochie@rocketmail.com wrote:

> Can anyone think of an efficient way to extract numbers from a string?
> 
> For example, given "Hey, 10-4 good buddy :)", is there a quick way to extract
> 10 and 4?

The programs included here won't do _exactly_ what you want, 
but they're a good starting point for the type of program 
that you need. The original program technique is taken from 
Gerald Squelart; the original concept was proposed by myself 
in a Mini-Challenge. HTH.

katz

:::::

STR2REAL.TXT
============
STR2REAL.APP is a PDL application for the HP48 S/SX/G/GX calculators. It 
uses a finite state machine approach to return the first number found in 
a text string. There are three programs in the application:

	S->I	Return the first Signed Integer   ( $ -> %signed )
	S->R	Return the first Real Number      ( $ -> %real )
	S->U	Return the first Unsigned Integer ( $ -> %unsigned )

There is no error checking for bad arguments; these programs were
written 
under the assumption that they will be run immediately after an INPUT
step 
in a program which requires a numerical input from the user. Each of the 
three programs is stack and flag neutral.

HOW STR2REAL WORKS
==================
The programs in STR2REAL examine the input string and use a finite state 
machine to determine the first and last characters of the desired
numerical 
substring. All processing is done on the stack in order to maximize
speed. 
The two integer programs are based on the S->R program, so only the S->R 
program is explained in detail; it is left to the reader to see how S->R 
was adapted to become S->I and S->U.

Each program searches the input string for characters contained in a
defined 
"component" string. This string is composed of all the characters that
are 
acceptable components of the number type to be returned:

	"+-.E0123456789"

Each character of the input string will change the state of the program 
according to two factors:

	1. The program's present state
	2. The category number of the current character

For each character of the input string, the program will generate a
state 
transition value based on two attributes of the current character:

	1. Is the character an acceptable number component?
	2. If yes, then which type of component is the character?

The possible characters are separated into five categories:

	1. Any digit (referred to as n)
	2. Any non-component character (referred to as x)
	3. Any sign character (+ or -)
	4. The decimal point (.)
	5. The exponent character (E)

There are twelve discrete states within the program:

	 0. Valid output number exists until the current character => Exit
	 1. Nothing valid yet
	 2. (+/-)
	 3. (+/-).
	 4. .
	 5. (+/-).n
	 6. (+/-)n
	 7. (+/-)n.
	 8. (+/-)n.n
	 9. Valid output number exists; current character is E
	10. Valid output number exists; current character is (+/-) after E
	11. Valid output number exists; current character is n after E

The program uses a one-dimensional list (representing the state-matrix) 
to determine the state transitions based on the current state and
character. 
The table below shows the structure and contents of the state matrix:


	 Current |                    Current State                     | Row
	  Char.  |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 | 10 | 11 |
	=========+====+====+====+====+====+====+====+====+====+====+====+=====
	    n    | -6 |  6 |  5 |  5 |  5 |  6 |  8 |  8 | 11 | 11 | 11 |  1
	    x    |  1 |  1 |  1 |  1 |  0 |  0 |  0 |  0 | .1 | .2 |  0 |  2
	    +    | -2 | -2 | -2 | -2 |  0 |  0 |  0 |  0 | 10 | .2 |  0 |  3
	    -    | -2 | -2 | -2 | -2 |  0 |  0 |  0 |  0 | 10 | .2 |  0 |  4
	    .    | -4 |  3 | -4 | -4 |  0 |  7 |  0 |  0 | .1 | .2 |  0 |  5
	    E    |  1 |  1 |  1 |  1 |  9 |  9 |  9 |  9 | .1 | .2 |  0 |  6
	=========+====+====+====+====+====+====+====+====+====+====+====+=====
	  Column |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 | 10 | 11 |


The program's next state is generated using the current state value and
the 
appropriate transition value obtained from the state list. The location
of 
the state transition value within the list is determined using a special 
algorithm which uses the dimensions of the state matrix as its
parameters.

:::::

// HP 48 Program Development Link Application
//
/Angle Radians
/FractionMark .
/InstallDir WORK
/AutoExec
//
/Var S\->I
@ Crack the FIRST signed integer from a string.
@ Created by: Jonathan M. Katz; initial idea from Gerald Squelart
@ Created on: 09 Jun, 1998

\<< " 0 " + 0 DUP 1
  WHILE DUP
  REPEAT 4 PICK ROT 1 + SWAP OVER DUP SUB
    "-0123456789" SWAP POS 1 + DUP 3 < * 3 * ROT +
    { -3  3  3
       1  1  0
      -2 -2  0 }
    SWAP GET
    IF DUP 0 <
    THEN NEG ROT DROP OVER SWAP
    END
  END
  DROP 1 - SUB STR\->
\>>
//
/Var S\->R
@ Crack the FIRST real number from a string.
@ Created by: Jonathan M. Katz; initial idea from Gerald Squelart
@ Created on: 09 Jun, 1998

\<< " 0 " + 0 DUP 1
  WHILE DUP IP
  REPEAT 4 PICK ROT 1 + SWAP OVER DUP SUB
    "+-.E0123456789" SWAP POS 1 + DUP 6 < * 11 * ROT +
    { -6  6  5  5  5  6  8  8 11 11 11
       1  1  1  1  0  0  0  0 .1 .2  0
      -2 -2 -2 -2  0  0  0  0 10 .2  0
      -2 -2 -2 -2  0  0  0  0 10 .2  0
      -4  3 -4 -4  0  7  0  0 .1 .2  0
       1  1  1  1  9  9  9  9 .1 .2  0 }
    SWAP GET
    IF DUP 0 <
    THEN NEG ROT DROP OVER SWAP
    END
  END
  DROP 1 - SUB STR\->
\>>
//
/Var S\->U
@ Crack the FIRST unsigned integer from a string.
@ Created by: Jonathan M. Katz; initial idea from Gerald Squelart
@ Created on: 09 Jun, 1998

\<< " 0 " + 0 DUP 1
  WHILE DUP
  REPEAT 4 PICK ROT 1 + SWAP OVER DUP SUB
    "0123456789" SWAP POS 1 + DUP 2 < * 2 * ROT +
    { -2  2
       1  0 }
    SWAP GET
    IF DUP 0 <
    THEN NEG ROT DROP OVER SWAP
    END
  END
  DROP 1 - SUB STR\->
\>>