在循环中定义数组名称
我可能会以错误的方式处理这个问题,但我正在尝试在循环内定义和填充数组。
for i = 0,39 do begin
xx = long(findgen(n+1l)*sx + line1x[i])
sz = size(xx)
arrayname = 'line' + strtrim(i,2)
arrayname = findgen(3,sz[1])
arrayname[0,*] = xx
arrayname[1,*] = yy
arrayname[2,*] = vertline
endfor
这显然行不通,但是有没有办法使用 'line' + strtrim(i,2) 定义的字符串在每次迭代时创建并填充一个新数组?在本例中,我有 40 个名称为 line0...39 的数组。这里困难的部分是 sz[1] 变化,所以我不能简单地定义一个大数组来容纳所有内容。
I may be going about this the wrong way, but I'm trying define and fill arrays within a loop.
for i = 0,39 do begin
xx = long(findgen(n+1l)*sx + line1x[i])
sz = size(xx)
arrayname = 'line' + strtrim(i,2)
arrayname = findgen(3,sz[1])
arrayname[0,*] = xx
arrayname[1,*] = yy
arrayname[2,*] = vertline
endfor
This obviously won't work, but is there a way to use the string defined by 'line' + strtrim(i,2) to create and fill a new array upon each iteration? In this case I'd have 40 arrays with names line0...39. The difficult part here is that sz[1] varies, so I can't simply define one large array to hold everything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 idl 8.0 或更高版本中,您可以使用
HASH
数据类型来实现此目的。您的代码如下所示:
您现在可以按名称访问数组:
In idl 8.0 or later you could use the
HASH
datatype for this.Your code would looks like this:
You can now access your arrays by name:
好吧,如果您想要进行肮脏的黑客攻击(并且不需要它在未经许可的虚拟机安装上运行),那么总是有
execute
函数。但是您是否考虑过声明一个一维指针数组,其中每个元素都指向 3 个 by sz 子数组之一?这给你带来了一个大数组的一些好处,
没有所有子数组必须具有相同形状的约束。
它可能看起来像这样......
Well, there's always the
execute
function, if you're in the mood for a filthy hack (and don't need it to run on an unlicensed virtual machine installation).But have you considered declaring a 1-D array of pointers, where each element points to one of your 3 by sz subarrays? That gives you some of the benefit of one big array,
without the constraint of all the subarrays having to have the same shape.
It might look something like this...