[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Removing elements from structures
From: |
Etienne Grossmann |
Subject: |
Removing elements from structures |
Date: |
Thu, 22 Nov 2001 22:08:38 +0000 |
From: Roberto Hernandez <address@hidden>
# Hi all,
Hello
# I've been looking through the documentation, but can't find how to
# remove an element from a structure.
# Suppose I define the following:
# a.name = "Bob"
# a.lastname = "Hope"
# a.phone = "555-5555"
I wrote a function rmfield () for that purpose.
octave:7> a = rmfield (a, "phone")
a =
{
name = Bob
lastname = Hope
}
# TIA,
You're welcome, if that's what you needed. Note however that the
method is not very efficient : it creates a new hash and copies
everything except the unwanted elements. See yourself :
======================================================================
## t = rmfield(s,key1,...)
##
## Removes key1, key2, ... from structure s.
## Return s if s is not a struct. Any better behavior?
##
## For m****b compatibility and flexibility.
##
## Basically, does a 'filtering' copy of s.
##
## See also cmpstruct, fields, setfield, isstruct, getfield, isfield,
## catstruct and struct.
##
## Author: Etienne Grossmann <address@hidden>
## Last modified: January 2000
function t = rmfield(s,...)
if ! is_struct(s) ,
t = s ;
return
end
va_start() ;
rmf = ' ' ;
nargin-- ;
while nargin-- ,
tmp = va_arg() ;
if ! isstr(tmp) ,
printf('struct: called with non-string key\n') ;
tmp
keyboard
else
rmf = [rmf,tmp,' '] ;
end
end
for [val, key] = s ,
if ! index(rmf,[' ',key,' ']) , % Check if key is wanted
eval(['t.',key,'=val;']) ; % Copy
end
end
======================================================================
This could easily be improved upon, but I just wanted something that
worked. That and other struct tools can be found at
http://www.isr.ist.utl.pt/~etienne/octave/
Hey! Even better, you can find it at Octave-forge (thanks Paul, for
putting it in)
http://octave.sf.net
Cheers,
Etienne
--
Etienne Grossmann ------ http://www.isr.ist.utl.pt/~etienne
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------