Added LOOKUP_INDEX and LOOKUP_VALUE to core.

This commit is contained in:
Steven Panek 2009-10-27 22:09:00 +00:00
parent c228abcffd
commit 9d493e2691

View file

@ -136,6 +136,44 @@
{CLEAR_VARIABLE REPEAT_i}
#enddef
#define LOOKUP_INDEX FROM_ARRAY WHERE_KEY WHERE_VALUE SAVE_AS
#! Call this to lookup an array element that has a particular key-value pair
#! then it saves the index of that element, or
#! if the key-value pair is not found it saves the array's length
{VARIABLE {SAVE_AS} 0}
[while]
[variable]
name={SAVE_AS}
less_than=${FROM_ARRAY}.length
[/variable]
[variable]
name={FROM_ARRAY}[${SAVE_AS}].{WHERE_KEY}
not_equals={WHERE_VALUE}
[/variable]
[do]
{VARIABLE_OP {SAVE_AS} add 1}
[/do]
[/while]
#enddef
#define LOOKUP_VALUE FROM_ARRAY WHERE_KEY WHERE_VALUE SAVE_KEY DEFAULT SAVE_AS
#! Call this to look up an array element that has a particular key-value pair
#! then it saves another key from that same element.
{LOOKUP_INDEX {FROM_ARRAY} {WHERE_KEY} {WHERE_VALUE} {SAVE_AS}}
[if]
[variable]
name={SAVE_AS}
numerical_equals=${FROM_ARRAY}.length
[/variable]
[then]
{VARIABLE {SAVE_AS} {DEFAULT}}
[/then]
[else]
{VARIABLE {SAVE_AS} ${FROM_ARRAY}[${SAVE_AS}].{SAVE_KEY}}
[/else]
[/if]
#enddef
#define DEBUG_MSG MESSAGE
# Emit a debug message. Meant to be overridden with no-op definition
# of the same name for production use.