I have a global table with bunch of columns and quite a few rows. I want to create a map from those. How can I do it in flexscript?
I have a global table with bunch of columns and quite a few rows. I want to create a map from those. How can I do it in flexscript?
Below you can find the code for a user command - mapFromTable() - where you pass in the table and optionally columns you want to use as your map keys. If you don't pass in any columns then it will assume the first column holds the key values.
For example, with this data:
running
Map m=mapFromTable("GlobalTable1",["Name","Height"]); return m[["Bob",175]].Zip;
will return 1234 - the shorter Bob's zip code;
The user command is:
Variant v=param(1); Variant k=param(2); Table t; switch (v.type) { case VAR_TYPE_STRING: case VAR_TYPE_NODE: t=Table(v);break; case VAR_TYPE_POINTER: t=v;break; default: msg("mapFromTable()", "Table type is "+v.type,1); return 0; } Array keys=[t.getColHeader(1)]; if (parqty>1) keys=param(2); Array attrCols=[]; for (int n=t.numCols;n>0;n--){ if (keys.indexOf(n)<0 && keys.indexOf(t.getColHeader(n))<0) attrCols.push(t.getColHeader(n)); } Map m; Map attrs; for (int r=t.numRows;r>0;r--){ Variant rowKey=[]; for (int k=1;k<=keys.length;k++) rowKey.push(t[r][keys[k]]); if (rowKey.length==1) rowKey=rowKey[1]; for (int n=attrCols.length;n>0;n--) { attrs[attrCols[n]]=t[r][attrCols[n]]; } m[rowKey]=attrs.clone(); attrs.clear(); } return m.clone();
Auto-installing library attached.
Updated for the case where one key column is used, to allow no longer require the single key to be in an array: eg. myMap["Bob"] instead of previously requiring myMap[["Bob"]]
16 People are following this question.
FlexSim can help you understand and improve any system or process. Transform your existing data into accurate predictions.
FlexSim is a fully 3D simulation software environment. FlexSim can be used to simulate any process in any industry.
FlexSim®, FlexSim Healthcare™, Problem Solved.®, the FlexSim logo, the FlexSim X-mark, and the FlexSim Healthcare logo with stylized Caduceus mark are trademarks of FlexSim Software Products, Inc. All rights reserved.
Privacy | Do not sell or share my personal information | Cookie preferences | Report noncompliance | Terms of use | Legal | © Autodesk Inc. All rights reserved