views:

30

answers:

2

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.

Thanks in advance for any help. Mike

A: 

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...

ptrs=ptrarray(40) ; Allocate an array of 40 pointers, initialized to null

for i = 0,39 do begin
  ; calculate sz, xx, yy, vertline
  dummy=findgen(3,sz[1])
  dummy[0,*] = xx
  dummy[1,*] = yy
  dummy[2,*] = vertline
  ptrs[i]=ptr_new(dummy) ; create copy of dummy on the heap, storing pointer in ptrs[i]

endfor

; To access the i-th subarray...

(*ptrs[i])[0,*] = new_xx
(*ptrs[i])[1,*] = new_yy
(*ptrs[i])[2,*] = new_vertline
Jim Lewis
A: 

Thanks Jim! That's what I was looking for.

Mike

Mike Smith
You're welcome...and welcome to StackOverflow! Please consider marking my answer as "accepted" by ticking the check mark next to it...it highlights the answer so future readers can see that it solved your problem, plus we both gain some sweet, sweet reputation points. :-)
Jim Lewis