From: Klaus Berndl
Subject: Convert Strings Into Numbers (integer, float)
Date: 
Message-ID: <4d0gk7$t63@news.rz.uni-passau.de>
My problem:

How to convert a suitable string (i.e. the string contents only digits and perhaps one dot) into a number-format like integer (only digits allowed) or float (only digits and one dot allowed)?
Exists there a function or a macro in Common Lisp (Allegro CL 4.2)?

Ciao,
      KLaus Berndl

e-mail: ······@fmi.uni-passau.de

From: k p c
Subject: Re: Convert Strings Into Numbers (integer, float)
Date: 
Message-ID: <1996Jan10.233410.5754@ptolemy-ethernet.arc.nasa.gov>
Quoth ······@vogelweide.uni-passau.de (Klaus Berndl):
> Exists there a function or a macro in Common Lisp (Allegro CL 4.2)?

You can use read-from-string and parse-integer.  read-from-string is
the comprehensive way to do it.  Look at the documentation for
with-standard-io-syntax and *read-eval*.

Note that my profiling shows that Allegro CL 4.2 read-from-string is
slow.

Consider a freeware parse-float that is floating around somewhere.  I
don't know if it is more efficient than read-from-string or not,
however.

By the way, please keep your line lengths below 77 characters.

If you post a followup to this article, I would appreciate a courtesy
verbatim copy by email to help work around potentially unreliable feeds.

---
···@ptolemy.arc.nasa.gov.  AI, multidisciplinary neuroethology, info filtering.
From: Michael Tselman
Subject: Re: Convert Strings Into Numbers (integer, float)
Date: 
Message-ID: <4d2dqd$c50@camelot.ccs.neu.edu>
Klaus Berndl (······@vogelweide.uni-passau.de) wrote:
: My problem:

: How to convert a suitable string (i.e. the string contents only digits and perhaps one dot) into a number-format like integer (only digits allowed) or float (only digits and one dot allowed)?
: Exists there a function or a macro in Common Lisp (Allegro CL 4.2)?

: Ciao,
:       KLaus Berndl

: e-mail: ······@fmi.uni-passau.de

In a standard Common Lisp you can use the function

-----------------------------------------------------------------------------
PARSE-INTEGER                                                      [Function]

Function in LISP package:
Args: (string
       &key (start 0) (end (length string)) (radix 10) (junk-allowed nil))
Parses STRING for an integer and returns it.

-----------------------------------------------------------------------------

Using it you can also easily write a parse-float function for the floats in 
your format.


--Misha

--
-----------------------------------------------------------------------------+
Michael Tselman (Misha)   Internet: ·····@ccs.neu.edu   | Imagination is     |
College of Computer Science, Northeastern University    |   more important   |
23 Cullinane Hall, 360 Huntington Ave., Boston, MA 02115|     than knowledge!|
Phone: (617)-373-3822,      Fax:  (617)-373-5121        |                    |
WWW: http://www.ccs.neu.edu/home/misha                  | (A. Einstein)      |
-----------------------------------------------------------------------------+
From: Peter Norvig
Subject: Re: Convert Strings Into Numbers (integer, float)
Date: 
Message-ID: <NORVIG.96Jan11173302@meteor.harlequin.com>
> Klaus Berndl (······@vogelweide.uni-passau.de) wrote:
> 
> : How to convert a suitable string (i.e. the string contents only digits and perhaps one dot) into a number-format like integer (only digits allowed) or float (only digits and one dot allowed)?


Here's a solution:

(defun parse-float (string)
  "Return a float read from string, and the index to the remainder of string."
  (multiple-value-bind (integer i)
      (parse-integer string :junk-allowed t)
    (multiple-value-bind (fraction j)
	(parse-integer string :start (+ i 1) :junk-allowed t)
      (values (float (+ integer (/ fraction (expt 10 (- j i 1))))) j))))

CL-USER> (parse-float "  123.456 ")
123.456
9
-- 
Peter Norvig                  | Phone: 415-833-4022           FAX: 415-833-4111
Harlequin Inc.                | Email: ······@harlequin.com
1010 El Camino Real, #310     | http://www.harlequin.com
Menlo Park CA 94025           | http://www.cs.berkeley.edu/~russell/norvig.html
From: Antonio Leitao
Subject: Re: Convert Strings Into Numbers (integer, float)
Date: 
Message-ID: <worax43z0n.fsf@poirot.ist.utl.pt>
>>>>> "Klaus" == Klaus Berndl <······@vogelweide.uni-passau.de> writes:

 Klaus> My problem:
 Klaus> How to convert a suitable string (i.e. the string contents only digits and perhaps one dot) into a number-format like integer (only digits allowed) or float (only digits and one dot allowed)?
 Klaus> Exists there a function or a macro in Common Lisp (Allegro CL 4.2)?

'read-from-string' solves your problem

Hope this helps

Antonio Leitao
From: Antonio Leitao
Subject: Re: Convert Strings Into Numbers (integer, float)
Date: 
Message-ID: <woohs83yt5.fsf@poirot.ist.utl.pt>
I forgot to add some documentation:

    FUNCTION
    read-from-string  -  read an object from a string

    Package LISP

    Usage
    read-from-string string [eof-error-p [eof-value [:start start]
      [:end end] [:preserve-whitespace preserve]]]

    DESCRIPTION
    Returns two values: the LISP object read from successive  characters  in
    string and the index of the first character in string that was not read.

    You can read from a substring of string by  specifying  values  for  the
    keyword  arguments  :start and :end.  The keyword argument :start speci-
    fies the index of the first character in  string  to  read.   Its  value
    defaults  to  0, denoting the beginning of string.  The keyword argument
    :end, specifies an index one greater than the index of the last  charac-
    ter  to  read.  Its value may be an integer greater than or equal to the
    value of the :start argument and less than or equal  to  the  length  of
    string,  or  nil.   The  value nil is the same as the default value, the
    length of string.

    If the value of the keyword argument :preserve-whitespace  is  specified
    non-nil,   white   space  will  be  preserved  in  the  same  manner  as
    read-preserving-whitespace.

    The argument eof-error-p controls what happens when the end of string is
    reached.   If  the  value of the argument is t, the default, an error is
    signaled.  However, if the value of the argument is nil, then  in  most
    situations  an  error  is  not  signaled. Instead, the read-from-string
    function terminates and  returns  the  value  of  eof-value.   eof-value
    defaults  to nil.  The function read-from-string always signals an error
    if the end of string is reached when a COMMON LISP object  is  partially
    but not completely read.

    EXAMPLES
    (read-from-string "this is a test") => this 5
    (read-from-string "this is a test" nil nil :preserve-whitespace t)
      => this 4
    (read-from-string "this is a test" nil 'done :start 5) => is 8
    (read-from-string "this is a test" nil 'done :start 5 :end 6)
      => i 6
    (read-from-string "this is a test" nil 'done :start 14) => done 14

Sorry about the two-part answer.
Good Luck.

Antonio Leitao
From: Antonio Leitao
Subject: Re: Convert Strings Into Numbers (integer, float)
Date: 
Message-ID: <wog2dh45n9.fsf@sol.gia.ist.utl.pt>
I am sorry because I have incorrectly posted some Franz, Inc
copyrighted documentation without permission.

I was trying to suggest the function read-from-string when I quoted
directly from Franz's CommonLisp documentation without even including
the copyright line.

Again, I am sorry. I never intended to get credit for the answer. I
just thought it would be helpful to include the function
documentation and completely forgot it was Franz's documentation and
not mine's.

Thanks for Erik Naggum for politely remind me of my fault (and suggest
a correction).

I will try to pay more attention in the future.

Antonio Leitao