X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 03 Mar 2015 12:03 PM by  anon
Creating variables/structures with a foreach loop
 2 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:23
New Member


--
03 Mar 2015 12:03 PM
    Hello,I want to create variable names (that I want to use later in the program) through a foreach loop. I want the variable names: dist100, dist200, dist300 etc. and assign an array to those (e.g. [99, 99]). Then I want to use those variables later on to do some further calculations, so I want to be able to e.g. add values to them to make them [99, 99, 34, 56, 87, 32] etc. Is there a simple way doing this?I also tried creating structures this way (see below):distarray = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600]catdislen = size(distarray, /n_elements); setting up the category namesforeach element, distarray do begin catdis = create_struct(name='catdis' + string(element, format='(i2.2)'), [[99,99]])endforeach...but that gives me an error. How would I create structures using the distarray above?Thanks,Mike

    Deleted User



    New Member


    Posts:
    New Member


    --
    09 Mar 2015 01:39 PM
    Hey Mike, Here are a couple of options that may help. You can use the StrTrim function to create the tag name. It may be useful to add each struct to a list to keep track of them: distarray = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600] distList = List() foreach element, distarray do begin catdis = Create_Struct(('catdis' + StrTrim(String(element),2)), [[99,99]]) distList.Add, catdis endforeach print, distList Or you could use an IDL hash to hold the data. Hashes can be very useful in cases like this: distarray = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600] distHash = Hash() foreach element, distarray do begin distHash['catdis' + StrTrim(String(element),2)] = [[99,99]] endforeach print, distHash Does this help?

    Deleted User



    New Member


    Posts:23
    New Member


    --
    19 Mar 2015 09:22 AM
    Thanks Scott. I'll give that a try.Mike
    You are not authorized to post a reply.