[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Octave-bug-tracker] [bug #40095] Clearvar function not implemented
From: |
Ceral Paquet |
Subject: |
[Octave-bug-tracker] [bug #40095] Clearvar function not implemented |
Date: |
Fri, 16 Mar 2018 08:45:35 -0400 (EDT) |
User-agent: |
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0 |
Follow-up Comment #6, bug #40095 (project octave):
I'd like to contribute this working implementation of clearvars.m based
heavily on what David Turner did earlier. It has one minor bug relating to
Octave's handling of ans in evalin (https://savannah.gnu.org/bugs/?53359).
++
function clearvars(varargin)
count = 0;
global_mode = false;
regexp_mode = false;
except_mode = false;
# parse argument list
for arg = 1:nargin
curr_arg = varargin{arg};
if strcmp(curr_arg, "-global")
global_mode = true;
continue;
endif
if strcmp(curr_arg, "-except")
except_mode = true;
continue;
endif
if strcmp(curr_arg, "-regexp")
regexp_mode = true;
continue;
endif
count += 1;
vars(count).except = except_mode;
vars(count).global = global_mode;
vars(count).regexp = regexp_mode;
vars(count).var_name = curr_arg;
endfor
# do we need a wildcard?
if count == 0 || all([vars.except])
count += 1;
vars(count).except = false;
vars(count).global = false;
vars(count).regexp = false;
vars(count).var_name = '*';
endif
# expand regular expressions
for c = 1:count
eval_str = 'who(';
if vars(c).global
eval_str = [eval_str '"global",'];
endif
if vars(c).regexp
if isempty(strcmp(vars(c).var_name,'*'))
vars(c).var_name = ['*' vars(c).var_name '*'];
endif
endif
eval_str = [eval_str '"' vars(c).var_name '")'];
# replace with cell array of matching strings
vars(c).var_name = evalin('caller',eval_str);
endfor
# clear variables one by one
for c1 = find([vars.except]==false)
for v1 = 1:numel(vars(c1).var_name)
# handle excepts (brute force)
do_clear = true;
for c2 = find([vars.except]==true)
for v2 = 1:numel(vars(c2).var_name)
do_clear &=
~isequal(vars(c1).var_name{v1},vars(c2).var_name{v2});
endfor
endfor
if do_clear == true
eval_str = 'clear ';
if vars(c).global
eval_str = [eval_str ' -global '];
endif
eval_str = [eval_str ' ' vars(c1).var_name{v1}];
evalin("caller",eval_str);
endif
endfor
endfor
endfunction
--
_______________________________________________________
Reply to this item at:
<http://savannah.gnu.org/bugs/?40095>
_______________________________________________
Message sent via/by Savannah
http://savannah.gnu.org/
- [Octave-bug-tracker] [bug #40095] Clearvar function not implemented,
Ceral Paquet <=