;+ ; NAME: ; struct_assignfield ; PURPOSE: (one line) ; assign a field to a value in an existing structure ; DESCRIPTION: ; assign a field to a value in an existing structure, changing type ; to type of passed variable. This allows us to ; CATEGORY: ; util ; CALLING SEQUENCE: ; new = struct_assignfield(old, tag, new_structname = new_structname) ; INPUTS: ; old - existing structure ; tag - name of field to delete (string) ; val - value for tag ; OPTIONAL INPUT PARAMETERS: ; none ; KEYWORD INPUT PARAMETERS: ; new_structname - if the structure is to be named (not anonymous), ; the name ; KEYWORD OUTPUT PARAMETERS: ; none ; OUTPUTS: ; new - new structure with field added ; COMMON BLOCKS: ; None ; SIDE EFFECTS: ; none ; RESTRICTIONS: ; PROCEDURE: ; calls struct_delfield and struct_addfield ; USAGE ; MODIFICATION HISTORY: ; 2005 Dec 31 Leslie Young ;- function struct_assignfield, old, tag, val, new_structname = new_structname n = n_elements(old) if n eq 1 then begin old_names = tag_names(old) match = where( strcmp(old_names, tag, /fold_case) eq 1, count ) if count ne 0 then begin tmp = struct_delfield(old, tag) new = struct_addfield(tmp, tag, val, $ new_structname = new_structname) endif else begin new = struct_addfield(tmp, tag, val, $ new_structname = new_structname) endelse return, new endif ; recursive call for old = array of structures new1 = struct_assignfield(old[0], tag, val[0], $ new_structname = new_structname) new = replicate(new1, n) for i=1, n-1 do begin new[i] = struct_assignfield(old[i], tag, val[i], $ new_structname = new_structname) endfor return, new end