将多个未指定长度的列表传递给程序
Passing multiple lists of unspecified length to a program
我想创建一个 Stata 程序,它将多个未指定长度的列表作为参数。但是,我不知道程序如何区分传入的列表。
例如,我希望能够执行如下操作:
prog myprog
args list1 list2
{something with list1}
{something with list2}
end
loc list1 a b c
loc list2 x y z
myprog `list1' `list2'
loc list1 a b c d
myprog `list1' `list2'
我一直在思考的两个解决方案是:
- 创建额外的宏来指定每个列表的长度并将它们也传入
- 在列表之间使用分隔符
两者都不是很困难,但我认为有一种更简单的方法可以做到这一点。
我正在为 Windows 使用 Stata 13。
以下对我有用:
program define myprog
syntax, list1(string) list2(string)
display "`list1'"
display "`list2'"
end
local lista "a b c d"
local listb "e f g h"
myprog, list1(`lista') list2(`listb')
或:
capture program drop myprog
program define myprog
tokenize `0', parse(";")
local list1 `1' // optional
local list2 `3' // optional
display "`list1'" // or "`1'"
display "`list2'" // or "`3'"
end
local lista a b c d
local listb e f g h
myprog `lista';`listb'
我想创建一个 Stata 程序,它将多个未指定长度的列表作为参数。但是,我不知道程序如何区分传入的列表。
例如,我希望能够执行如下操作:
prog myprog
args list1 list2
{something with list1}
{something with list2}
end
loc list1 a b c
loc list2 x y z
myprog `list1' `list2'
loc list1 a b c d
myprog `list1' `list2'
我一直在思考的两个解决方案是:
- 创建额外的宏来指定每个列表的长度并将它们也传入
- 在列表之间使用分隔符
两者都不是很困难,但我认为有一种更简单的方法可以做到这一点。
我正在为 Windows 使用 Stata 13。
以下对我有用:
program define myprog
syntax, list1(string) list2(string)
display "`list1'"
display "`list2'"
end
local lista "a b c d"
local listb "e f g h"
myprog, list1(`lista') list2(`listb')
或:
capture program drop myprog
program define myprog
tokenize `0', parse(";")
local list1 `1' // optional
local list2 `3' // optional
display "`list1'" // or "`1'"
display "`list2'" // or "`3'"
end
local lista a b c d
local listb e f g h
myprog `lista';`listb'