结构

来自Shiyin's note
Shen讨论 | 贡献2016年11月14日 (一) 01:05的版本
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

匿名和命名结构

  • 结构一旦命名不能在程序中修改
IDL> A={Stru1,Tag1:'AA'}
IDL> B={Stru1,Tag1:'AA',Tag2:'BB'} (会报错)


结构(数组)的生成和修改

sdssidl里面有一组程序,可以更好的生成和编辑结构,且完全适用于结构数组

结构(数组)的生成

str = mrd_struct(['fld1', 'fld2'], ['0','dblarr(10,10)'],3)

调用create_struct,但是远比create_struct方便

combine结构:COMBINE_STRUCTS

Combine 2 equal length arrays of structures into one array of structures, with the UNION of the tags. Values from the common tags will be from the first structure.

CALLING SEQUENCE:

 combine_structs, struct_array1, struct_array2, new_struct


重命名Tags:rename_tags

newstruct = rename_tags(struct, oldtagnames, newtagnames)

适用于同时重命名多个Tags

增加Tags: add_tag和add_tags

  • add_tag 一次增加一个tag (不能直接赋值)
add_tag, oldstruct, tagname, tagtype, newstruct, structyp=structyp

structyp: a string with the name of the new structure. f already defined the program will crash.

  • add_tags 可以同时增加多个Tag (不能直接赋值)
tagnames=['ra', 'dec', 'image', 'name']
values  =['0d', '0d',  'intarr(1000, 1000)', "'NGC3035'"]
add_tags, oldstruct, tagnames, values,new_struct

参见jadd_tag的程序[1]

删除Tags: remove_tags

newstruct = remove_tags(oldstruct, tagnames) 可以同时移除多个Tags,tagname是个数组即可

其它

  • 结构很容易增加一个tag,比如
A= {tag1:0.,tag2:'Name'}
B=create_struct(A,'tag3',1L)
  • 结果数组经常被用来读入数据文件,如果该数据文件加上一列数据,如何较方便的操作。
一种方法如下:
A= {tag1:0.,tag2:'Name'}
B= {tag1:1.,tag2:'Name2',tag3:10L}
A2=  replicate(A,10)
B2 = replicate(B,10)
struct_assign,A,B ;把A的数值付给B
print,B ;可以看到B的前两列已经变成A的数值,但B的第三列被充0,所以这时候可以再给B.tag3赋值。
还有一种方法,但没上面那么直观
str={a: 0l, b: }
str_arr=REPLICATE(str,10)
ref_str = reform_struct(str_arr, /tag_array, 10)
ref_str = create_struct(ref_str, 'c' ,make_array(10))
help, ref_str, /str