How to parse text with numbers and string
Let’s say that you have a file with numbers and text, separated by a space, and you want to read it into APDL arrays and strings.
/inquire,MY_LINES,lines,filename.txt
This reads the number of lines in the text file filename.txt and places that value in a scalar parameter MY_LINES. This allows us to dynamically obtain the length of the arrays (i.e., each line has two entries, number and text).
Next, the arrays will be dimensioned as follows:
*dim,STRING_DATA,string,128,MY_LINES
*dim,ARRAY_DATA,array,MY_LINES
Data can then be read and put into the string STRING_DATA:
*sread,STRING_DATA(1),filename.txt
The data containing number and text is read into each index of the string array STRING_DATA. Now, the data needs to be parsed using string functions; in this case, a space character is assumed to delimit the text:
*do,AR80,1,MY_LINES
AR70 = strpos(STRING_DATA(1,AR80),’ ‘)
ARRAY_DATA(AR80) = valchr(strsub(STRING_DATA(1,AR80),1,AR70))
AR71 = strleng(STRING_DATA(1,AR80))-AR70+1
STRING_DATA(1,AR80) = strsub(STRING_DATA(1,AR80),AR70,AR71)
*enddo
Looping through the number of lines (items in the array), the string position of the first occurrence of the space character (‘ ‘) is found. The string is then cut up to that space – assuming that the first entry is a number, this is converted to from a string to number with the valchr() function. Then, the last portion of the string is cut and put back into itself.
This is just a simple example of using string functions, /INQUIRE, and *SREAD commands to read arbitrary data from a text file and parse it accordingly.
Of course, if the data in a text file to be read only contains numbers, *TREAD or *VREAD are easier alternatives requiring no string functions.
hey, sheldon, great post. I wonder how can this be implemented using Tcl/Tk. ( Ansys can use Tcl/Tk calls, rigth? ) shouldn´t be easier to implented using tcl?
cesar …
ps. I enjoy every blog entry!
Hi Cesar,
Thanks for your posting.
Tcl/Tk interaction with ANSYS can be found on this page:
http://ansys.net/?mycat=uf_tcl
Specifically, the last document on the above page (PDF doc) has the ANSYS API for Tcl/Tk.
You can use the generic “ans_sendcommand()” to send ANSYS commands from Tcl once you’ve parsed the data and values in Tcl (i.e., use commands like *DIM or *SET in ans_sendcommand to send the values from Tcl/Tk to ANSYS).
If you’re already familiar with Tcl/Tk, it may be easier (and, for tons of data, faster) to do all the work in Tcl. Then, call the script (e.g., “file.tcl”) with the APDL comands “eui,’source file.tcl'” (no double quote).
(Not everyone is familiar with Tcl/Tk, so that’s why I showed the above approach using APDL.)
Regards,
Sheldon
Sheldon!
thanks for the reply and the link, there is a lot of useful information. 😀
lookin’ forward for your next blog post.
regards,
cesar