开发者

mumps lanaguage FOR loop related

if i define For K="ABC":1:3 now what is the value of "ABC",how the loop to excute "ABC" or instead of th开发者_JAVA技巧at ABC if i take any "string", how does increment the string "ABC".

Bhas


MUMPS converts strings to numeric values by reading through the string from left to right. It will use any numbers and the first period it comes across in the resultant number, and it will stop at the first non-numeric character.

Examples of string to number conversions:

String -> Number
"1234" -> 1234
"1234A" -> 1234
"1234A4321" -> 1234
"" -> 0
"A" -> 0
"ABC" -> 0


If you have access to a MUMPS system, it's trivial to find out. Just run the following routine, which could reside e.g. in a file for3.m:

for3

; routine to test FOR command

   FOR K="ABC":1:3 WRITE "K=",K,! QUIT

On my Linux box, I have GT.M installed. You can get it at http://sourceforge.net/projects/fis-gtm/

Here's the output:

$ gtm -run for3
K=0
K=1
K=2
K=3

HTH Nathan


If you want to iterate over the string "ABC" you can do this:

S STR="ABC"
F I=1:1:$L(STR) W $E(STR,I,I),!

the $L ($Length) function will return the length of STR. In this case 3, so the for loop will only iterate 3 times.

The $E ($Extract) function will extract a substring of STR. The first parameter to $E is the string to extract from. The second parameter is the start position, and the third parameter is the ending position of the substring. In this case, I am specifying the same starting and ending position so that each character will be extracted one at a time.

You can also specify a field separator as the second parameter to the $L function. So if the STR="Name|Age|Sex" you could write out each field by:

S STR="NAME|AGE|SEX"
F I=1:1:$L(STR,"|") W $P(STR,"|",1),!

Here $L would return 3 and the for loop would iterate 3 times.

The $P function ($Piece) will pull out each field from STR using the pipe character ( | ) as the field delimiter and I determines which field to return.

Hope this helps...

igotmumps


Very sorry for the munged formatting. Mea culpa!

The program listing should have been:

for3

; routine to test FOR command

   FOR K="ABC":1:3 WRITE "K=",K,! QUIT

And the output should have been:

$ gtm -run for3 
K=0 
K=1 
K=2
K=3
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜