HMath-3000: Working with HyperMath – String Library |
![]() |
![]() |
![]() |
![]() |
|
HMath-3000: Working with HyperMath – String Library |
![]() |
![]() |
![]() |
![]() |
In this tutorial, strings will be evaluated and manipulated using HyperMath. In the first example, a string within a string will be found and then replaced. In the second example, an element from a matrix will be formatted.
Step 1: Launch HyperMath.
1. | From the Start menu, select All Programs > Altair HyperWorks > HyperMath. |
This launches HyperMath in the HyperMath GUI. Notice that by default, a file named Untitled1.hml exists in the Editor window. By default, the Authoring Mode is displayed as well.
Step 2: Define a global variable which contains the string.
1. | From the Command window, type the following command: |
string1="There are ten people and ten dogs"
2. | Press ENTER on the keyboard to evaluate the command. |
3. | Type the following to review the variable string1. |
print(string1)
4. | Again, press ENTER on the keyboard to evaluate the command. The following is printed in the Command window: |
There are ten people and ten dogs
Step 3: Use the string.find command to find the string ten.
1. | In the Command window, type the following command: |
print(string.find(string1, "ten"))
This prints the output from the string.find command to the Command window.
2. | Press ENTER on the keyboard to evaluate the command. This results in the following output: |
11 | 13 |
The output from this command indicates that the first instance of the string "ten" starts at index 11 and ends at index 13.
3. | This output can also be assigned to a variable, as shown in the following commands. Type the following into the Command window: |
output1a, output1b=string.find(string1, "ten")
4. | Press ENTER to evaluate the command. To view the output, use the print command. Type the following into the Command window. |
print(output1a, output1b)
5. | Press ENTER to evaluate the command. This results in the following output: |
11 | 13 |
Step 4: Find any other references to "ten" in the string.
1. | Now that the beginning and ending index of the first instance of the string "ten" has been found, we can search for any other instances of the string. This is done using the following command in the Command window: |
output2a, output2b=string.find(string1, "ten", output1b)
The command above assigns the beginning index to output2a and the ending index to output2b. It uses the variable output1b to start the search of the variable string1 for the string "ten".
2. | Press ENTER to evaluate the command. |
3. | Type the following in the Command window to view the output from the command above. |
print(output2a, output2b)
4. | Press ENTER to evaluate the command. This results in the following output: |
26 | 28 |
5. | Now extract the last found substring. Type the following in the Command window to view the output from the command above. |
print(string.sub(string1,output2a, output2b+1))
Remember, in the string.sub command, the second index that is specified indicates where the substring should end at, not including that second index. Therefore, we need to add one to the value of output2b to ensure that the index in output2b is included in the substring.
6. | Press ENTER to evaluate the command. This results in the following output: |
ten
7. | Again, search for any additional instance of the string "ten" after the index defined by variable output2b. |
output3a, output3b=string.find(string1, "ten", output2b)
8. | Press ENTER to evaluate the command. |
9. | Type the following in the Command window to view the output from the command above: |
print(output3a, output3b)
10. | Press ENTER to evaluate the command. This results in the following output: |
nil | nil |
This indicates that there is not a third instance of the string "ten".
Step 5: Use the substitution command string.gsub to replace the string ten with the number 10.
1. | In the Command window, type the following command: |
string2=string.gsub(string1, "ten", "10", 2)
2. | Press ENTER to evaluate the command. |
3. | Type the following in the Command window to view the output from the command above: |
print(string2)
4. | Press ENTER to evaluate the command above. This results in the following output: |
There are 10 people and 10 dogs
Step 6: Define a matrix, format an element, and assign it to a variable.
1. | In the Command window, type the following command to define the matrix A: |
A=[1.2345, 6.7891; 6.7891, 1.2345]
2. | Press ENTER to evaluate the command. |
3. | Type the following in the Command window to view the output from the command above: |
print(A)
4. | Press ENTER to evaluate the command above. This results in the following output: |
[Matrix] 2 x 2
1.2345 6.7891
6.7891 1.2345
5. | Type the following in the Command window to create a variable A12 which contains a formatted element from the A matrix: |
A12=string.format("%6.2f", A(1,2))
6. | Press ENTER to evaluate the command. |
7. | Type the following in the Command window to print the contents of the variable A12: |
print(A12)
8. | Press ENTER to evaluate the command above. This results in the following output: |
6.79
The value has been formatted so that there are only 2 decimal points.
HMath-1000: Editing, Executing, Saving, and Plotting in HyperMath
HMath-1010: Working with HyperMath Authoring Mode
HMath-1020: Working with HyperMath Debugging Mode
HMath-2000: Working with HyperMath – Arithmetic and Relational Expressions and Control Structures
HMath-2010: Working with HyperMath – Logical and Relational Expressions and Control Structures
HMath-2020: Working with HyperMath – Functions and Matrix Operators
HMath-2030: Working with HyperMath – Plot Commands
HMath-3010: Working with HyperMath – Input/Output Library
HMath-3020: Working with HyperMath – Input/Output Library Continued
HMath-3030: Working with HyperMath – Batch Mode
HMath-4000: Using HyperMath Functions for Curve Fitting
HMath-4001: Using HyperMath for Material Characterization
HMath-4010: Solving Ordinary Differential Equations
HMath-4020: Solving Differential Algebraic Equations
HMath-4030: Optimization Algorithms in HyperMath
HMath-5000: Using HyperMath in HyperView Results Math
HMath-5001: Post Processing Results from FEA
HMath-5002: Registering a Function in HyperGraph 2D
HMath-5003: HyperMesh-HyperMath Cross Execution of a Tcl Script
HMath-5004: HyperMesh-HyperMath Cross-debugging of a Tcl Script