Dear All,
just wanted to share my experience (and, perhaps, confirm that my conclusions are correct).
Consider running the following code (example for this discussion, the production code is more complex, but that complexity is not relevant to the below):
which is exactly:
I expected that I will get a positive confirmation for every element of the list, over which I am iterating, but I've got surprising results:
Noticing that the problem occurs for every multi-word country name, I have checked against the documentation , but it confirmed that adorned values are acceptable as elements of the list and will be treated as a single value for all list-manipulation routines:
It appears that in this case the straightforward way is not correct, since -foreach- will remove the adornment present in the list, and subsequent probing will see non-adorned values (in local c), which will be interpreted as a list again, and for the case of multi-word items as list of so many single word items, resulting in incorrect (unintended) evaluation. This was somewhat non-obvious and could have been easily overlooked (if not the attention of my colleague, who was triple-checking the results).
It is all clear now, and the following version of the code produces the correct results with an extra step added:
which is exactly:
I believe there is no way to avoid this extra step in this case (the green line), e.g. by somehow reformulating the line that follows (through options, parentheses, or similar elements), but if there is one, please, do let me know.
Best regards and Happy New Year!
Sergiy Radyakin
just wanted to share my experience (and, perhaps, confirm that my conclusions are correct).
Consider running the following code (example for this discussion, the production code is more complex, but that complexity is not relevant to the below):
Code:
do "http://www.radyakin.org/statalist/2024/2024-01-12_list_in.do"
Code:
local countries = `""Botswana" "Comoros" "Eswatini" "Ethiopia" "Kenya" "Lesotho" "Madagascar" "Malawi" "Mauritius" "Mozambique" "Namibia" "Rwanda" "South Africa" "South Sudan" "Sudan" "São Tomé and Principe" "Tanzania" "Uganda" "Zambia" "Zimbabwe""' foreach c in `countries' { local res : list c in countries display as text `"`c'"' _col(30) `"---> "' as result `"`res'"' } // END OF FILE
Code:
Botswana ---> 1 Comoros ---> 1 Eswatini ---> 1 Ethiopia ---> 1 Kenya ---> 1 Lesotho ---> 1 Madagascar ---> 1 Malawi ---> 1 Mauritius ---> 1 Mozambique ---> 1 Namibia ---> 1 Rwanda ---> 1 South Africa ---> 0 South Sudan ---> 0 Sudan ---> 1 São Tomé and Principe ---> 0 Tanzania ---> 1 Uganda ---> 1 Zambia ---> 1 Zimbabwe ---> 1
A list is a space-separated set of elements listed one after the other. The individual elements may be enclosed in quotes, and elements containing spaces obviously must be enclosed in quotes.
It is all clear now, and the following version of the code produces the correct results with an extra step added:
Code:
do "http://www.radyakin.org/statalist/2024/2024-01-12_list_in_works.do"
Code:
local countries = `""Botswana" "Comoros" "Eswatini" "Ethiopia" "Kenya" "Lesotho" "Madagascar" "Malawi" "Mauritius" "Mozambique" "Namibia" "Rwanda" "South Africa" "South Sudan" "Sudan" "São Tomé and Principe" "Tanzania" "Uganda" "Zambia" "Zimbabwe""'
foreach c in `countries' {
local cc `""`c'""'
local res : list cc in countries
display as text `"`c'"' _col(30) `"---> "' as result `"`res'"'
}
// END OF FILE
Best regards and Happy New Year!
Sergiy Radyakin
Comment