在设置变量时使用变量

发布于 2025-01-03 23:39:39 字数 230 浏览 1 评论 0原文

我想这样枚举一个变量:

$x = 0
Do
  $x+=1
  $Day$x = True
Until $x = 7

上面的代码在 $Day$x 上返回一个语法错误(因为它只应该在命令中看到一个变量)。所以基本上,我想要 $Day1 = True$Day2 = True 等等。有办法做到这一点吗?

I want to enumerate a variable as so:

$x = 0
Do
  $x+=1
  $Day$x = True
Until $x = 7

The above returns a syntax error on $Day$x (because it's only supposed to see one variable in the command). So basically, I want $Day1 = True, $Day2 = True, so on and so forth. Is there a way to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下客 2025-01-10 23:39:39

欢迎来到奇妙的数组世界。

#include <Array.au3> ; for debugging

Global $Day[7]
$x = 0
Do
    $Day[$x] = True
    $x+=1
Until $x = 7

_ArrayDisplay($Day) ; For debugging

实际上,您可以使用 $Day1、$Day2、$Day3 执行最初描述的操作,但这将是一种非常糟糕的编程实践,我个人强烈建议不要这样做。话虽这么说,使用赋值函数是可以实现的。你会因为完全错误的原因而使用它。但为了完整起见,方法如下:

$x = 0
Do
    Assign("Day" & $x, True)
    $x+=1
Until $x = 7

MsgBox(0, "", $Day2)

Welcome to the wonderful world of arrays.

#include <Array.au3> ; for debugging

Global $Day[7]
$x = 0
Do
    $Day[$x] = True
    $x+=1
Until $x = 7

_ArrayDisplay($Day) ; For debugging

You can actually do what you originally described with $Day1, $Day2, $Day3 but it would be a very bad programming practice and I personally strongly discourage it. That being said, it is possible with the Assign function. You'd be using it for totally the wrong reason. But for completeness, here's how:

$x = 0
Do
    Assign("Day" & $x, True)
    $x+=1
Until $x = 7

MsgBox(0, "", $Day2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文