Search code examples
structurevariable-assignmentidl

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.


Solution

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