首页/文章/ 详情

HyperWork 2024 -二次开发-Tcl语言-Arrays(3/12)

27天前浏览202

The array is a frequently occurring variable type in programming languages. In Tcl, arrays complement lists by providing additional capabilities when organizing data. Arrays are Tcl variables that have string-based indices (names). Any string value can be used as an array index.

Tcl arrays are used similarly to the way that "struct" is used in C.

set arrData(this\ is\ a\ test) "HyperWorks";
set indexvar "this is a test";
set arrData($indexvar) "HyperWorks";
set arrData(this,is,a,test) "HyperWorks";

These are all valid array definitions, but note the escape character \ used in the first version to prevent issues with spaces. Unlike brackets, parentheses do not affect how the string is interpreted and will therefore not prevent the white spaces from causing problems. The first two versions are equivalent, and will be evaluated as the same variable in the Tcl environment. The third case is different because the index is not equivalent to the string with spaces. As the index is a string, commas can be used in the indices, though they are not required. The third example illustrates using commas to delimit indices.

The array command is used in Tcl to manipulate array data. The following list contains some commonly used array commands and a summary of their usage. For more in-depth explanations and a complete listing, referred to http://www.tcl.tk/man/ or to a Tcl/Tk handbook.

  • array exists 

  • Returns  if the array exists,  otherwise.

  • array get 

  • Converts an array of format arrName(index1) containing value1 to a list of format:

  • index1 value1 index2 value2…

  • The pattern function allows a subset of indices to be selected using search capabilities as in the string match command.

  • array names 

  • Returns a list of indices matching pattern. If no pattern is given, returns all indices for the array. Pattern matching is the same as the string match command.

  • array set 

  • Converts a list of format:

  • index1 value1 index2 value2…

  • to arrName(index1) containing value1.

  • array size 

  • Returns the number of elements in an array or 0 if the array does not exist or is empty.

  • array unset 

  • Unsets elements of an array matching , or all elements in the array if no pattern is given. Pattern matching is the same as the string match command.

Array variables are useful for related data because it is possible to search through arrays and loop through array name-value pairs.

set curve(color) 5;
set curve(name) "Curve 1";
set curve(display) "on";

To return array data by using the array name:

puts $curve(color);
5

or

set name "color";
puts $curve($name);
5

To find which names are available in the array:

array names curve
name display color

Arrays can also have complex names. An example is creating an array of x,y,z coordinates for a node in the model.

set xcoord 0.0;
set ycoord 1.0;
set zcoord 2.0;
set coords(1,x) $xcoord;
set coords(1,y) $ycoord;
set coords(1,z) $zcoord;

The Tcl environment comes with some default arrays. One of the more useful default arrays is the env array to obtain environment variable settings.

array names env;
Tcl returns a list of environment variables
puts "$env(Path)";
Tcl returns the Path environment variable
来源:TodayCAEer
ACT二次开发UGUM
著作权归作者所有,欢迎分享,未经许可,不得转载
首次发布时间:2024-08-14
最近编辑:27天前
TodayCAEer
本科 签名征集中
获赞 16粉丝 8文章 163课程 0
点赞
收藏
作者推荐

HyperWork 2024 -二次开发-Tcl语言-Math(5/12)

InTcl,mathematicalexpressionsareevaluatedusingtheexprcommand.Thiscommandworkswithintegers,floatingpointvaluesandlogical(Boolean)values.Therearemanyarithmeticoperatorsandbuilt-inmathfunctionsinTcl.Formorein-depthexplanationsandacompletelisting,referredtohttp://www.tcl.tk/man/ortoaTcl/Tkhandbook.Anexampleofcalculatingthecircumferenceofacirclewithradiusof6:setcircumference[expr2*(2*asin(1.0))*6.0]puts$circumference37.6991118431Variablereferencescanbeusedwiththeexprcommandaswell.setpi[expr2*asin(1.0)]setradius6.0setcircumference[expr2*$pi*$radius]puts$circumference37.6991118431Itisalsopossibletousenestedcommandsinsideofexpr.setpi[expr2*asin(1.0)]setradius"radius"setcircumference[expr2*$pi*[stringlength$radius]]puts$circumference37.6991118431Itisimportanttonoticethedifferencebetweenusingintegervaluesandfloatingpointvalueswhendoingarithmeticoperations.setusing_integer[expr1/9]setusing_float[expr1/9.]puts$using_int0puts$using_float0.111111111111ItisrecommendedpracticetousecurlybracestogroupexprcommandsandimprovetheefficiencyoftheTclbytecodecompiler.Thishelpstopreservetheprecisionthatmaybelostbyconvertingbetweenstringsandnumericvalues.Thisinturnspeedsuptheevaluationoftheexpression.setcircumference[expr{2*(2*asin(1.0))*6.0}]setpi[expr{2*asin(1.0)}]setradius6.0setcircumference[expr{2*$pi*$radius}]Italsoeliminatesthepossibilityofnotgettinganexpectedvaluereturnedduetotheprecisionlostinthestringtonumericvalueconversion.setvar1[expr{sqrt(2.0)}]puts$var11.41421356237setvar2[expr$var1*$var1]puts$var21.99999999999setvar2[expr{$var1*$var1}]puts$var22.0InHyperWorks,thedefaultnumberofsignificantdigitsreturnedis12.TheTclvariableallowsyoutoqueryandsetthenumberofsignificantdigitsreturned.puts$tcl_precision12setvar1[expr1/9.]0.111111111111settcl_precision17puts$tcl_precision17setvar1[expr1/9.]0.11111111111111111来源:TodayCAEer

未登录
还没有评论
课程
培训
服务
行家
VIP会员 学习 福利任务 兑换礼品
下载APP
联系我们
帮助与反馈