Comparing elements of two different local macros

2019-07-23 19:52发布

I have a local macro called peer_list that contains 280 distinct elements, all of which are strings. I also have another local macro called used_list that contains a subset of the elements contained in the local peer_list.

For each element in peer_list I would like to test whether that element is in the local used_list. If the element exists in used_list I would like to discard it, otherwise I would like to execute another set of conditions.

I have tried to use the following code but it hasn't worked:

foreach peer in local peer_list {
    if `:list peer in local used_list' {
        * commands I wish to execute
    }
    else {
        * commands I wish to execute
    }
}

I would appreciate any advice on alternative ways of accomplishing this.

1条回答
迷人小祖宗
2楼-- · 2019-07-23 20:13

You don't say in what sense your code "hasn't worked" and you don't provide a reproducible example. Nevertheless you appear to be working along the right lines.

 local beasts frog toad newt unicorn griffin 
 local real frog toad newt 

 foreach b of local beasts { 
     if `: list b in real' { 
         di "`b' is real" 
     } 
    else di "`b' is fabulous" 
 } 

 frog is real
 toad is real
 newt is real
 unicorn is fabulous
 griffin is fabulous

A common bug with similar code is to define and use local macros in different locales so that they cannot see each other.

A more obvious bug is that you need the keyword of not in. What you have is legal, but not what you want. Compare

foreach b in local beasts {
    di "`b'"
}

local
beasts
查看更多
登录 后发表回答