|
Re: delete a tagname from a structure [message #9790 is a reply to message #9715] |
Thu, 21 August 1997 00:00   |
Alex Schuster
Messages: 124 Registered: February 1997
|
Senior Member |
|
|
This is a multi-part message in MIME format.
--------------1BA853742701
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Some days ago, in an article I don't have here, R. Bauer asked for such
a routine. Now I needed something like this for myself and wrote this
small routine I've attached here. It replaces a tag from a structure by
another, and also allows its deletion. Seems to work, although I don't
guarantee for anything...
Alex
--
Alex Schuster Wonko@weird.cologne.de PGP Key available
alex@pet.mpin-koeln.mpg.de
--------------1BA853742701
Content-Type: text/plain; charset=us-ascii; name="replace_tag.pro"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="replace_tag.pro"
;+
; NAME:
; REPLACE_TAG
;
; PURPOSE:
; Replaces a tag in a structure by another
;
; CALLING:
; replace_tag, struct, old_tag, new_tag, value
;
; INPUTS:
; struct - the structure to be changed
; old_tag - name of the tag to be changed
; new_tag - name of the new tag
; If set to '', old_tag is deleted from the structure
; value - value of the new tag
;
; RESTRICTIONS:
; Does not work with named structures
;
; MODIFICATION HISTORY:
; Alex Schuster, MPIfnF, 8/97 - Written
;-
pro replace_tag, struct, old_tag, new_tag, value
tags = tag_names( struct )
pos = (where( tags eq strupcase( old_tag ) ))(0)
if ( pos eq -1L ) then begin
print, 'Error: Tag ', old_tag, ' not in struct.'
return
endif
if ( ( pos eq 0 ) and ( new_tag ne '' ) ) then begin
new_struct = create_struct( new_tag, value )
endif else begin
new_struct = create_struct( tags(0), struct.(0) )
for i = 1, pos-1 do $
new_struct = create_struct( new_struct, tags(i), struct.(i) )
if ( new_tag ne '' ) then $
new_struct = create_struct( new_struct, new_tag, value )
endelse
for i = pos+1, n_elements( tags )-1 do $
new_struct = create_struct( new_struct, tags(i), struct.(i) )
struct = new_struct
end
--------------1BA853742701--
|
|
|
Re: delete a tagname from a structure? [message #86002 is a reply to message #9715] |
Mon, 23 September 2013 14:55   |
spluque
Messages: 33 Registered: September 2013
|
Member |
|
|
Hi,
Searching for a way to remove a field from a structure, I found this advice from 1997. Is there a better way to do this nowadays, in the most general way suggested by JD Smith below?
Thanks,
Seb
On Friday, August 8, 1997 2:00:00 AM UTC-5, J.D. Smith wrote:
> R. Bauer wrote:
>>
>> Hi,
>>
>> I like to remove a tagname from a structure.
>>
>> Any ideas?
>>
>> --
>> R.Bauer
>>
>> Institut fuer Stratosphaerische Chemie (ICG-1)
>> Forschungszentrum Juelich
>> email: R.Bauer@fz-juelich.de
>
>
> st={tag1:1,tag2:2,tag3:3,tag4:4} ; a structure
> tags=tag_names(st)
> list=[0,1,3] ;; list of which elements to keep
> st=create_struct(tags[list],st.(list[0]),st.(list[1]),st.(li st[2]))
>
>
> If you have a named struct, you can use
> name=tag_names(st,/STUCTURE_NAME) and create_struct(name=name,...)
>
> For a totally general method which doesn't preassume the number of tags
> to keep, you'd have to build an 'execute' statement.
>
> E.g.
>
> n=strtrim(n_elements(list),2)
> exc='st=create_struct(tags[list],'+string(FORMAT='('+n+'("st.(list[ ",I0,"])",:,","))',indgen(n_elements(list)))+')'
> out=execute(exc)
>
> JD
|
|
|
Re: delete a tagname from a structure? [message #86011 is a reply to message #86002] |
Tue, 24 September 2013 01:44   |
Fabzi
Messages: 305 Registered: July 2010
|
Senior Member |
|
|
Hi
On 09/23/2013 11:55 PM, spluque@gmail.com wrote:
> Searching for a way to remove a field from a structure,
I found this in my lib, I don't remember who wrote the code in the first
place but it's not that complex:
;+
; :Description:
; Removes a tag from a structure.
;
; :Params:
; struct: in, required
; the structure
; tagname: in, required, type=string
; the tag name
;
;-
pro w_remove_tag, struct, tagname
; Set Up environnement
COMPILE_OPT idl2
if n_params() ne 2 then MESSAGE, 'N_PARAMS=2'
searchtag=strupcase(tagname)
tagnames=tag_names(struct)
a=[-1]
if n_elements(tagname) eq 1 then a=[a,where(tagnames $
ne searchtag)] else for i=0,n_elements(tagnames)-1 do $
if (where(searchtag eq tagnames[i]))[0] eq -1 then a=[a,i]
if n_elements(a) eq 1 then return
if a[1] eq -1 then return
newstruct=create_struct(tagnames[a[1]],struct.(a[1]))
if n_elements(a) gt 2 then for i=2,n_elements(a)-1 $
do newstruct=create_struct(newstruct,tagnames[a[i]],struct.(a[i ]))
struct=newstruct
end
|
|
|
Re: delete a tagname from a structure? [message #86036 is a reply to message #9715] |
Wed, 25 September 2013 02:33   |
ameigs
Messages: 12 Registered: March 2009
|
Junior Member |
|
|
On Friday, August 8, 1997 8:00:00 AM UTC+1, R. Bauer wrote:
> Hi,
>
> I like to remove a tagname from a structure.
>
> Any ideas?
>
>
> --
> R.Bauer
>
> Institut fuer Stratosphaerische Chemie (ICG-1)
> Forschungszentrum Juelich
> email: R.Bauer@fz-juelich.de
I use the undocumented keyword to CREATE_STRUCT "REMOVE= tag_position". To make it more palatable I use Craig's cmset_op to get the index of the tag(s) as in the procedure below:
PRO agm_remove_tag, thisstruct, tagnames
;; first find the indices of the tag names you want to remove
tags = tag_names(thisstruct)
indx = cmset_op(tags, 'AND', STRUPCASE(tagnames), /index, COUNT = count)
IF count NE 0 THEN BEGIN
thisstruct = CREATE_STRUCT(thisstruct, remove = indx)
ENDIF
END
|
|
|
Re: delete a tagname from a structure? [message #86037 is a reply to message #86036] |
Wed, 25 September 2013 07:45  |
spluque
Messages: 33 Registered: September 2013
|
Member |
|
|
On Wednesday, September 25, 2013 4:33:40 AM UTC-5, ameigs wrote:
> On Friday, August 8, 1997 8:00:00 AM UTC+1, R. Bauer wrote:
>
>> Hi,
>
>>
>
>> I like to remove a tagname from a structure.
>
>>
>
>> Any ideas?
>
>>
>
>>
>
>> --
>
>> R.Bauer
>
>>
>
>> Institut fuer Stratosphaerische Chemie (ICG-1)
>
>> Forschungszentrum Juelich
>
>> email: R.Bauer@fz-juelich.de
>
>
>
> I use the undocumented keyword to CREATE_STRUCT "REMOVE= tag_position". To make it more palatable I use Craig's cmset_op to get the index of the tag(s) as in the procedure below:
>
>
>
> PRO agm_remove_tag, thisstruct, tagnames
>
> ;; first find the indices of the tag names you want to remove
>
> tags = tag_names(thisstruct)
>
> indx = cmset_op(tags, 'AND', STRUPCASE(tagnames), /index, COUNT = count)
>
> IF count NE 0 THEN BEGIN
>
> thisstruct = CREATE_STRUCT(thisstruct, remove = indx)
>
> ENDIF
>
> END
Thanks for these great suggestions.
Seb
|
|
|