The Altair Community is migrating to a new platform to provide a better experience for you. In preparation for the migration, the Altair Community is on read-only mode from October 28 - November 6, 2024. Technical support via cases will continue to work as is. For any urgent requests from Students/Faculty members, please submit the form linked here
[Solved] Reading numerical values with a script works ok but no text
Dear all,
I wonder why I cannot read out any text with the "execute script" operator. It's possible for values and it's possible to write text but not to read it... The following example code is supposed to simply copy the text from column "str" to "str2".
Please advice!
Kind regards
Sachs
I wonder why I cannot read out any text with the "execute script" operator. It's possible for values and it's possible to write text but not to read it... The following example code is supposed to simply copy the text from column "str" to "str2".
Please advice!
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<process version="6.1.000">
<context>
<input/>
<output/>
<macros/>
</context>
<operator activated="true" class="process" compatibility="6.1.000" expanded="true" name="Process">
<process expanded="true">
<operator activated="true" class="generate_data" compatibility="6.1.000" expanded="true" height="60" name="Generate Data" width="90" x="45" y="30">
<parameter key="number_examples" value="5"/>
<parameter key="number_of_attributes" value="1"/>
</operator>
<operator activated="true" class="generate_attributes" compatibility="6.1.000" expanded="true" height="76" name="Generate Attributes" width="90" x="179" y="30">
<list key="function_descriptions">
<parameter key="str" value=""text""/>
<parameter key="str2" value=""""/>
</list>
</operator>
<operator activated="true" class="execute_script" compatibility="6.1.000" expanded="true" height="76" name="Execute Script" width="90" x="380" y="30">
<parameter key="script" value="import javax.swing.JOptionPane; ExampleSet exampleSet = input[ 0 ]; Attribute att = exampleSet.getAttributes().get( "str" ); Attribute att2 = exampleSet.getAttributes().get( "str2" ); for (Example example : exampleSet ) { String s = example.getValue( att ); example.setValue( att2, s ); } return exampleSet;"/>
</operator>
<connect from_op="Generate Data" from_port="output" to_op="Generate Attributes" to_port="example set input"/>
<connect from_op="Generate Attributes" from_port="example set output" to_op="Execute Script" to_port="input 1"/>
<connect from_op="Execute Script" from_port="output 1" to_port="result 1"/>
<portSpacing port="source_input 1" spacing="0"/>
<portSpacing port="sink_result 1" spacing="0"/>
<portSpacing port="sink_result 2" spacing="0"/>
</process>
</operator>
</process>
Kind regards
Sachs
0
Answers
Strings are stored in a nominal mapping which exists for each attribute. This is basically a map from an int key to a String. So the actual data contains the double (in this case an int) key instead of the String.
If you want to get Strings, you will have to call example.getValueAsString() if it's nominal. To write, you have to first store the value in the mapping via attribute.getMapping().mapString(value) and then store the returned int (as double) in the example.
Regards,
Marco
Now I understand why RM always returned numbers instead of the string
Cheers
Sachs