【问题标题】:Passing multiple lists of unspecified length to a program将多个未指定长度的列表传递给程序
【发布时间】:2019-04-22 03:53:53
【问题描述】:

我想创建一个 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'

我一直在考虑的两个解决方案是:

  1. 创建额外的宏来指定每个列表的长度并将其传入
  2. 在列表之间使用分隔符

两者都不是很困难,但我认为有更简单的方法来做到这一点。

我正在使用适用于 Windows 的 Stata 13。

【问题讨论】:

    标签: list arguments stata stata-macros


    【解决方案1】:

    以下对我有用:

    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'
    

    【讨论】:

      猜你喜欢
      • 2022-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-11
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      相关资源
      最近更新 更多