< Back to IRCAM Forum

How to convert String to integer / float in OpenMusic

Dear Community,
I need a little bit of help.
I’m frustrating I can not find the right Function.
I have to read a .txt File into my OpenMusic Patch to take some calculations.
But it seams that the File reader from:
http://support.ircam.fr/docs/om/om6-manual/co/ReadingWriting.html
takes the line as a String.
And I need a float or a integer.
But I can not found any Functions for Conversion.

Can anybody help me a little bit

Dear Creco,

Since you didn’t specify in which context you need oto do this, here is the most general way to convert strings into integer/floats. However if in your string there is a non integer/float it still works…

Screenshot_2019-12-17_17-03-12

Best
K

Ok,
But I have;
(“123” “234” “345”)
and I need
(123 234 345 )
In which way I can have the first translate to the Second

Ok,
I have found a Solution.
But I think is a little bit complicated.
I have interrupted the Connection between the Line-Reader (“file-read-line”) and the Collector.
Then I have set a Funcall object and a String Object [parse-float].
So it works.
But is there any better Idea.

Ok now here it is:

Ok,
this is now clear,
a big thanks.

Hi,
I’m a bit late… but here’s another way.
Screenshot_2019-12-24_02-21-23

best regards,
rc

! correct !
string-to-number a custom OM function!
however, will not work with characters like “toto”

Great, thx rc

K

Hi,

Another alternative would be to use the LISP function read-from-string, for any given string its first output is the desired element (e.g., from “c4” or “flow” read-from-string ouputs [c4 2] and [flow 4], respectively.)

All the best,
António

Hi,

I’ve attached a LISP file with two alternative ways of doing it, which may be useful in different contexts:

  1. The first one is called string-to-atom, and transforms a string as follows: “string-content” --> string-content;
  2. The second one is string-to-list, and transforms a string as follows: “string-content” --> (string-content).

The code:

(defmethod string-to-atom ((box string))
	 (car (list (read-from-string box))))

(defmethod string-to-atom ((box list))
	 (loop for item in box
         collect (string-to-atom item)))

(defmethod string-to-list ((box string))
	 (list (read-from-string box)))

(defmethod string-to-list ((box list))
	 (loop for item in box
         collect (string-to-list item)))

The file, which you can put in your workspace’s “user” sub-folder: string-utils.lisp (445 Bytes)

Hope this helps.

All the best,
António

1 Like

Hallo,
I am trying to covert a string to number/integer. (E.g. ”23” => 23). In my earlier patches (made in earlier OM versions) I was using LISP function read-form-string and it was functioning well. Yet, since I changed to OM 7.2 and MacBook M2 (Ventura 13) this doesn’t function any more. OM crashes every time I evaluate the patch containing this function. (I use it for converting output from text-view to number/integer in order to pass it forward.) I also tried with LISP function string-to-number and parse-integer but unsuccessfully. If somebody has a solution please let me know!

All the best!

Aliser

Dear Aliser,

Can you send the patch please so I can check it out?
I don’t think it is a conversion issue.

Best
K