We value your input!
Please participate in Archicad 28 Home Screen and Tooltips/Quick Tutorials survey

Libraries & objects
About Archicad and BIMcloud libraries, their management and migration, objects and other library parts, etc.

Converting a string to a number

Anonymous
Not applicable
I am using STRSUB (myparameterstring, 1, 2) to extract string like "14" that I would like to convert to numbers. I have tried everything I could think of but nothing is working. I have tried to use an array to avoid a few hundred IF statements to no avail. Any ideas would be greatly appreciated.

TIA
Jeffrey
6 REPLIES 6
Jochen Suehlo
Moderator
You must use SPLIT to split parts of strings into numbers.
http://www.selfgdl.com/7_function/str_funktion/SPLIT/kompendium.php

e.g. n = SPLIT ("15.25", "%n", num_val)
num_val returns the number value of "15.25"
Jochen Suehlo . AC12-27 . MAC OSX 14.4 . WIN11
GDL object creation: b-prisma.de
Anonymous
Not applicable
Thanks Joachim,

I have been trying that but then I no longer have a string. Instead of just reading the appropriate parts it destroys the original string therefore destroying the value list it comes from and I get an "Error in expression..".

myparameterstring is a value list that represents part of an alphanumeric ID, it only contains 4 numbers i.e. "1224" that represent two dimensions (length and width) STRSUB was the easiest way I found to separate the first two numbers from the last. There are a few hundred variations. "1224", "1226", "1424", "1426"...

myparameterstring is in the parameter list as text (ABC) and needs to be used elsewhere.

nmbrvl = STRSUB(myparameterstring, 1, 2)

wdth = SPLIT ("nmbrvl", %n, num)

oawdth = wdth * 12"

The ultimate goal is to change it to a dimension.

Is there any way to prevent split from destroying the string?

Thanks

AC 9
Anonymous
Not applicable
Jeffrey wrote:
Is there any way to prevent split from destroying the string?
The whole point of SPLIT is to convert from string to number so the string aspect of the value will be lost. As long as the string is preserved in the original variable what is the problem?
The ultimate goal is to change it to a dimension.
For this you will need to convert the extracted number (which I assume is in inches) to meters since this is what GDL uses internally. Something like:
wdth = SPLIT ("nmbrvl", %n, num) * 0.0254
To later display this as a dimension would require STR with the appropriate format string.
Jochen Suehlo
Moderator
Jeffrey,
if you want to convert a string into a number, you must destroy the string.
But: you can use a new variable, so you do not have to destroy the original string.
The syntax of your script was not complete correct.
The variable before SPLIT is a dummy, not the returned value (it is 0 or 1),
that shows if SPLIT returns a value or not.
Try this script:

VALUES "myparameterstring" "1224", "1226", "1424", "1426"

nmbrvl = STRSUB(myparameterstring, 1, 2)

TEXT2 0,0,nmbrvl

x = SPLIT (nmbrvl, "%n", num)

oawdth = num * 12"

TEXT2 0,-0.1,oawdth
TEXT2 0,-0.2,x

I hope, I understood you right. But you cannot use the same variable/parameter both as string and as number.
Jochen Suehlo . AC12-27 . MAC OSX 14.4 . WIN11
GDL object creation: b-prisma.de
Jochen Suehlo
Moderator
Matthew,
if I try this:
wdth = SPLIT (nmbrvl, "%n", num) * 0.0254
(the quotes must be around %n)
TEXT2 0,-0.4,wdth
it returns 0.0254.
Please try instead:
x = SPLIT (nmbrvl, "%n", num)
oawdth = num * 12"
TEXT2 0,-0.1,oawdth 
If I calculate manually, the TEXT2 shows the correct result (in meters).
GDL is able to interpret the Inch sign.
Jochen Suehlo . AC12-27 . MAC OSX 14.4 . WIN11
GDL object creation: b-prisma.de
Anonymous
Not applicable
Thanks Matthew, Joachim,

I need the string to remain intact because it is part of a value list of predetermined sizes and is also used elsewhere in the script. The objects properties and other dimensions change depending on the size.

Joachim, It worked like a charm. I was thinking the new number was ???=SPLIT. Once I added the quotations to the %n and replaced num with wdth it all worked perfectly and the string stays intact.

The end result is.

nmbrvl = STRSUB (myparameterstring, 1, 2)

SPLIT (nmbrvl, "%n", wdth)

oawdth = wdth * 12"

The UI did show as meters but all I had to do was change "aowdth" to a formatted string.
Thanks again.