Nokie's Array

From Gwen Morse's Wiki
Jump to: navigation, search
;<--------------------------------------------------------------------------->
;< array.tf
;<
;< Written by: Nokie Quickfingers (jeff at billimek dot com)
;< http://billimek.com/sojourn/array.tf.html
;< 01/23/2002
;<--------------------------------------------------------------------------->

;<---------------------------------->
;< <INTERNAL> - used to clear array.
;<---------------------------------->
/def purge_vars = \
        /let purge_vars=$(/listvar -s %{*})%;\
        /let off=$[strchr(purge_vars," ")]%;\
        /while (off>-1) \
           /unset $[substr(purge_vars,0,off)]%;\
           /let purge_vars=$[substr(purge_vars,off+1)]%;\
           /let off=$[strchr(purge_vars," ")]%;\
        /done%;\
        /unset %purge_vars%;

;<---------------------------------->
;< Returns an value for an index.
;< Usage: /aget <array_name> <index>
;<---------------------------------->
/def aget = \
        /test value:=\{array_%{1}___%{2}\}%;\
        /return value

;<------------------------------------------->
;< Inserts into array for index.
;< Usage: /aput <array_name> <index> <value>
;<------------------------------------------->
/def aput = \
        /set array_%{1}___%{2}=%{-2}%;\
        /if (astart({1})>{2}) /test array_%1___min_len:={2}%; /endif%;\
        /if (alen({1})<{2}) /test array_%1___max_len:={2}%; /endif

;<----------------------------->
;< Clears out an array.
;< Usage: /aclear <array_name>
;<----------------------------->
/def aclear = \
        /purge_vars array_%1___*%;\
        /set array_%1___max_len=0%;\
        /set array_%1___min_len=$[0+error]

;<--------------------------------->
;< Returns the length of an array.
;< Usage: /alen <array_name>
;<--------------------------------->
/def alen = \
        /return \{array_%{1}___max_len\}

;<-------------------------------------->
;< Returns the first index in the array
;< Usage: /astart <array_name>
;<-------------------------------------->
/def astart = \
        /return \{array_%{1}___min_len\}

;<-------------------------------->
;< Displays all info for an array.
;< Usage: /ashow <array_name>
;<-------------------------------->
/def ashow = \
        /listvar -g array_%{1}___*

;<----------------------------------->
;< Displays all elements in an array
;< Usage: /asshow <array_name>
;<----------------------------------->
/def asshow = \
        /let acount=$[alen({1})+1]%;\
        /let res=%;\
        /while (--acount>=astart({1})) \
          /let res=$[aget({1},acount)] %res%;\
        /done%;\
        /echo %res

;<--------------------------------------------------->
;< Compares all elements in the array with argument.
;< Usage: /acomp <array_name> <argument to compare>
;<--------------------------------------------------->
/def acomp = \
        /let aforEachcount=$[astart({1})] %;\
        /let aforEachcount_max=$[alen({1})] %;\
        /let found_cmp=0 %;\
        /while (++aforEachcount<=aforEachcount_max) \
                /if (!found_cmp) \
                        /test value:=strcmp(tolower({2}),tolower(aget({1},aforEachcount))) %;\
                        /if (!value) \
                                /let result=1 %;\
                                /let found_cmp=1 %;\
                        /else \
                                /let result=0 %;\
                        /endif %;\
                /endif %;\
        /done %;\
        /return {result}

;<---------------------------------------------------->
;< Loops through the array and does whatever you pass.
;< Usage: /aforEach <array_name> <command>
;<---------------------------------------------------->
/def aforEach = \
        /let aforEachcount=$[astart({1})]%;\
        /let aforEachcount_max=$[alen({1})]%;\
        /while (++aforEachcount<=aforEachcount_max) \
           /eval -s0 %-1 $[aget({1},aforEachcount)]%;\
        /done