Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
839 views
in Technique[技术] by (71.8m points)

variable assignment - Create structures in loop and assign to another structure in IDL

In IDL I would like to create a structure in a loop with different data and then to assign them all to the main structure that will zip everything. I tried to used an array of structures, but I am stopped because I am not able to assign to the main structure:

alarm_list = { rg : 0, rf : 4}

alarm = { $  
          Alarm_Id : 0 , $  
          Range :      1, $
          Bin : 0        $ 
        }

arr = REPLICATE(alarm, 4)  
FOR ia = 0, 3 DO BEGIN
    alarm.alarm_id = ia
    alarm.bin = bin
    arr[ia] = alarm
    bin += 1
ENDFOR

I would like to assign all 4 alarms with different names (i.e. alarm1 = , alarm2 = ...) to the main "alarm_list". Thank you for your answers.

question from:https://stackoverflow.com/questions/65924262/create-structures-in-loop-and-assign-to-another-structure-in-idl

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could try something along these lines, assigning to the array directly instead of using the alarm variable as an intermediate:

alarm_list= REPLICATE({alarm, Alarm_Id:0, Range:1, Bin:0},4)

FOR ia = 0, 3 DO BEGIN
    alarm_list[i].alarm_id = ia
    alarm_list[i].bin = bin
    bin += 1
ENDFOR

Hope this helps guide.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...