I previously wrote
> Is there anything that is analogous to valof except that it takes
> a section as well as a word, and returns (or updates) the valof
> the word in that section.
Roger Evans drew my attention to the procedure word_identifier,
(described in REF SECTIONS) which does not do exactly what I want, but
is close enough.
word_identifier(word, sect, context) -> word_id [procedure]
This procedure enables sectioned identifiers to be represented
by unique word records.
Using this we can have the following defintion of section_valof
define section_valof(word,sect);
lvars word, sect;
valof(word_identifier(word,sect,undef))
enddefine;
define updaterof section_valof(word,sect);
lvars word, sect;
-> valof(word_identifier(word,sect,undef))
enddefine;
;;; test it
global vars sec1;
section sec1;
current_section -> sec1;
vars x1 = 66;
endsection;
section_valof("x1", sec1) =>
** 66
33 -> section_valof("x1", sec1);
section sec1;
x1 =>
** 33
endsection;
In my timing tests this version of section_valof is about 4 times as
slow as valof. That's not bad at all, and a LOT faster than the versions
I previously suggested.
It would be possible also to define this utility:
define section_identof(word, sect);
lvars word, sect;
identof(word_identifier(word,sect,undef))
enddefine;
section_identof("x1", sec1)=>
** <ident 33>
No doubt if these things were built in they could be marginally
faster.
I am not sure whether they should all take an extra "context" argument
analogous to the third argument of word_identifier, which I've here
set to undef, to prevent the errors that would arise due to attempting
to access identifiers not yet declared in the section. But that may be
the wrong default.
Aaron
|