Detecting the type of entry in a mixed array

25 views
Skip to first unread message

Markus Klug

unread,
Nov 14, 2022, 12:43:00 PM11/14/22
to Jaamsim Users Discussion Group
Hi,

I am very sorry, if I have overseen something that may answer my question, but I haven't found anything.
If - for any reason, there is an one-dimensional array consisting of numbers and strings, is there a command to discover is a particular element is a string or an entry.

E.g. having an attribute as following:

{ Gates  '{0, 1, 2, 3, 4, 5, "A001", "A002", "A003", "A004", "A005"}' }

if there's something to distinguish between numerical elements and strings.

I wouldn't do this consciously, but when importing data from an external source, unfortunately the numbers are not put into quotation marks and therefore converted to numbers, where anything that starts with a letter remains as a string.

Thanks!

Harry King

unread,
Nov 14, 2022, 2:14:46 PM11/14/22
to Jaamsim Users Discussion Group
Klaus,

There is no functions available at present that test the type of an argument. Functions such as 'isString' and 'isNumber' would be good additions to the expression system.

In the meantime, you can convert each input to a string using the format function, e.g. 'format("%s", x)' returns the representation of 'x' as a string. Note, however, that 'format("%s", 1)' returns "1.0", not "1". Use the 'map' function to convert an entire array in this way, i.e. 'map(|x|(format("%s", x)), array)', which returns an array of strings.

Harry

Markus Klug

unread,
Nov 15, 2022, 2:30:38 AM11/15/22
to Jaamsim Users Discussion Group
Dear Harry,

thanks a lot for your reply. You are doing a great job in developing and providing JaamSim and I think this workaround is of excellent help. So this seems to remain of minor importance.

Best,
Markus

Harry King

unread,
Nov 27, 2022, 6:58:10 PM11/27/22
to Jaamsim Users Discussion Group
Markus,

Release 2022-07 includes a new function 'typeName' that returns "STRING" if the argument is a string and "NUMBER" if it is a numerical value.

Harry

Reply all
Reply to author
Forward
0 new messages