Tcl 数组名称的可预测响应顺序?
我知道[数组名称X]返回的名称处于未定义的顺序,但它们总是处于相同未定义的顺序吗?
我正在处理一个非常大的数组,并且希望将进度记录到文件中,以防发生崩溃,这样我就可以再次中途恢复。
附言。 快速实验意味着它始终是相同的“随机”顺序,但这并不意味着它是真的!
I know that the names returned by [array names X] are in an undefined order, but are they always in the same undefined order?
I'm going through a very large array, and would like to log progress to a file in case of a crash, so I can resume part-way through again.
PS. A quick experiment implies it is always the same 'random' order, but this doesn't mean it's true!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的答案是,您不能依赖顺序,最好的选择是 [lsort [数组名称 X]] 并使用该顺序。
长的答案是,只要密钥相同(并且 Tcl 版本相同),顺序就应该稳定......但我仍然不会依赖它。
如果您使用 Tcl 8.5 或更高版本,您可能需要考虑使用 Dict 而不是数组。 Dict 元素的顺序是它们添加的顺序。
The short answer is that you can't rely on the order and your best bet is to [lsort [array names X]] and use that order.
The long answer is that the order should be stable as long as the keys are the same (and its the same Tcl version)... but I still wouldn't rely on it.
If you're using Tcl 8.5 or later, you might want to look at using a Dict instead of an array. The order of elements for a Dict is the order they were added in.