Format Conversion

From Planimate Knowledge Base
Jump to navigation Jump to search

When assigning to an attribute/cell using '=', Planimate treats the Right Hand Side as a number unless it is a literal string, in which case it performs automatic format conversion by parsing (interpreting) the string to get a numerical value. Eg:

p.att = 123 // assigns 123
p.att = "1d" // assigns 86400 (seconds in a day)
p,att = 122 + 1 // evaluates numerical expression, assigns 123
p.att = "12" & "3" // string concatenats to get "123" and assigns 123

If the RHS is an attribute or cell, its numerical value is always assigned. For label formatted attributes, this is the label index. For text formatted attributes and cells, it is important to realise that these retain the internal numerical value, even though it is unused and remains zero in string operations, which set a separate text part of the attribute only.

Hence if p.mytext is a Text formatted attribute:

p.mytext $= "1" & "2" & "3"  // sets p.mytext's text to "123"
p.att = p.mytext // assigns from the internal numerical value, which is zero.

However by converting p.mytext to a literal string, you can make Planimate interpret its string text as a value. This can be done in several ways:

p.mytext $= "123" // string text following lines work with
p.att = p.mytext & ""  // assigns 123
p.att = strclean(p.mytext) // assigns 123
p.att = strright(p.mytext,1) & 'd' => assigns 259200 seconds (text is "3d")

The reason this works is that the formatted string of the attribute is converted to a temporary literal string, so the conversion rule above applies and the '=' operation parses the string to get the value. strclean() is a good choice to force a literal string conversion.

This technique also works with Label formatted attributes, enabling the label text to be interpreted numerically rather than retrieving the label index.

Combined with string extraction such as strleft(), strright() and strmid() as well as string concatenation using '&', you can perfom sophisticated string to number conversions.

Technical Note:

The reason the assignment operator '=' does not automatically perform string to text conversion for attributes and cells (even those formatted as Text) is that Planimate is primarilly a numerical simulator using an interpretive runtime engine, hence for speed, numerics are assumed for data unless it is a literal string, in which case string to value conversion (which is time expensive) is performed.