Format Conversion: Difference between revisions

From Planimate Knowledge Base
Jump to navigation Jump to search
(Created page with "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 conversi...")
 
No edit summary
 
Line 1: Line 1:
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:
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<br>p.att = "1d"&nbsp;// assigns 86400 (seconds in a day)<br>p,att = 122 + 1 // evaluates numerical expression, assigns 123<br>p.att = "12"&nbsp;&amp; "3"&nbsp;// string concatenats to get "123"&nbsp;and assigns 123
p.att = 123 // assigns 123<br>p.att = "1d"&nbsp;// assigns 86400 (seconds in a day)<br>p,att = 122 + 1 // evaluates numerical expression, assigns 123<br>p.att = "12"&nbsp;&amp; "3"&nbsp;// string concatenats to get "123"&nbsp;and assigns 123  


If the RHS&nbsp;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.
If the RHS&nbsp;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:
Hence if p.mytext is a Text formatted attribute:  


p.mytext $= "1"&nbsp;&amp;&nbsp;"2"&nbsp;&amp;&nbsp;"3"&nbsp; // sets p.mytext's text to "123"<br>p.att = p.mytext // assigns to the internal numerical value, which is zero.
p.mytext $= "1"&nbsp;&amp;&nbsp;"2"&nbsp;&amp;&nbsp;"3"&nbsp; // sets p.mytext's text to "123"<br>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:
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<br>p.att = p.mytext &amp; ""&nbsp; // assigns 123 - explained below<br>p.att = strclean(p.mytext) // assigns 123<br>p.att = strright(p.mytext,1) &amp;&nbsp;'d' =&gt;&nbsp;assigns 259200 seconds (text is "3d")
p.mytext $= "123" // string text following lines work with<br>p.att = p.mytext &amp; ""&nbsp; // assigns 123<br>p.att = strclean(p.mytext) // assigns 123<br>p.att = strright(p.mytext,1) &amp;&nbsp;'d' =&gt;&nbsp;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. This technique also works with Label formatted attributes, enabling the label text to be interpreted numerically rather than retrieving the label index.
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.


Combined with string extraction such as strleft(), strright() and strmid() as well as string concatenation using '&amp;', you can perfom sophisticated string to number conversions.
This technique also works with Label formatted attributes, enabling the label text to be interpreted numerically rather than retrieving the label index.<br>


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


The reason the assignment operator '=' does not automatically perform string to text conversion for attributes and cells (even those formatted as Text)&nbsp;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)&nbsp;is performed automatically.
'''Technical Note:'''


[[Category:Attribute]]
The reason the assignment operator '=' does not automatically perform string to text conversion for attributes and cells (even those formatted as Text)&nbsp;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)&nbsp;is performed.
[[Category:Data]]
 
[[Category:Format (Units)]]
[[Category:Attribute]] [[Category:Data]] [[Category:Format_(Units)]] [[Category:Routine]]
[[Category:Routine]]

Latest revision as of 19:47, 13 December 2013

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.