[TF] Arrays
Brought to you by:
kenkeys
|
From: tinyfugue at attbi.c. (Galvin) - 2003-01-13 02:12:52
|
I made some changes and added some new functions to arrays, I overhauled
the code as well.
new functions
tfwrite_array() : write an array to disk
tfread_array() : read an array from disk
strstr_array() : search for a string in an array
list_array() was listarray() : good for debugging, lists arrays.
changes:
strstr_array() replaces get_array_count()
x := (strstr_array("my array", 0, 1000, '"") -1) will do the same
thing as:
get_array_count("my array", 0)
strstr_array() returns -1 if it doesn't find the element
list_array now uses /listvar to get the data to list, it refmats it to
make
it look like the old listarray.
if you find any bugs let me know, thanks.
==========================
-------------- next part --------------
;mylib.tf
;This contains functions that are used by other .TF files.
;=========================================================
;========================================================================
; get_array() / get_2array()
;
; Stores data into a virtual array
; USAGE:
; x := get_array("array_name", index)
; x := get_2array("array_name", index, index2)
;
; x : Value returned
; "array_name" : Name of the array
; index : Element
; index2 : Element of index (get_2array only)
;========================================================================
/def get_array = \
/return _%1_array_%2
/def get_2array = \
/return _%1_array_%2_%3
;========================================================================
; put_array() / put_2array()
;
; Gets data from a virtual array
; USAGE:
; x := get_array("array_name", index, value)
; x := get_2array("array_name2", index, index2, value)
;
; x : value returned
; "array_name" : Name of the array
; index : Element
; index2 : Element of index (get_2array only)
; value : Data you're placing into the array. Can be anything you want.
;========================================================================
/def put_array = \
/set _%1_array_%2=%3
/def put_2array = \
/set _%1_array_%2_%3=%4
;========================================================================
; tfwrite_array() / tfwrite_2array()
;
; Writes a virtual array to a disk file.
; USAGE:
; tfwrite_array("array_name", start_index, size, file_variable)
; tfwrite_2array("array_name", index, start_index, size, file_variable)
;
; "array_name" : Name of the array
; start_index : The element you want to start writing from.
; index : Start_index of index [first dimension] (tfwrite_2array only)
; Size : Number of elements to write.
; file_variable : File variable of a tfopened file.
;
; NOTES: tfwrite_2array() can't write the whole array to disk. You must
; write each dimension at a time.
;========================================================================
/def tfwrite_array = \
/test TFWA_Array := {1} %;\
/test TFWA_Start := {2} %;\
/test TFWA_Size := {3} %;\
/test TFWA_File := {4} %;\
/test TFWA_Count := -1 %;\
/WHILE (++TFWA_Count <= TFWA_Size) \
/test tfwrite(TFWA_File, get_array(TFWA_Array, TFWA_Start)) %;\
/test ++TFWA_Start %;\
/DONE
/def tfwrite_2array = \
/test TFWA2_Array := {1} %;\
/test TFWA2_Index := {2} %;\
/test TFWA2_Start := {3} %;\
/test TFWA2_Size := {4} %;\
/test TFWA2_File := {5} %;\
/test TFWA2_Count := -1 %;\
/WHILE (++TFWA2_Count <= TFWA2_Size) \
/test tfwrite(TFWA2_File, get_2array(TFWA2_Array, TFWA2_Index, TFWA2_Start)) %;\
/test ++TFWA2_Start %;\
/DONE
;========================================================================
; tfread_array() / tfread_2array()
;
; reads an array file from disk
; USAGE:
; x := tfread_array("array_name", start_index, size, file_variable)
; x := tfread_2array("array_name", index, start_index, size, file_variable)
;
; x : Number of records read
; "array_name" : Name of the array
; start_index : Starting element you want to read into.
; index : Start_index of index [first dimension] (tfread_2array only)
; size : Number of elements to read
; file_variable : File variable of a tfopened file
;========================================================================
/def tfread_array = \
/test TFRA_Array := {1} %;\
/test TFRA_Start := {2} %;\
/test TFRA_Size := {3} %;\
/test TFRA_File := {4} %;\
/test TFRA_Count := 0 %;\
/test TFRA_Done := 0 %;\
/test TFRA_Err := 0 %;\
/test TFRA_St := '' %;\
/WHILE (TFRA_Done = 0) \
/test TFRA_Err := tfread(TFRA_File, TFRA_St) %;\
/IF ( (TFRA_Err != -1) & (TFRA_Count < TFRA_Size) ) \
/test put_array(TFRA_Array, TFRA_Start, TFRA_St) %;\
/test ++TFRA_Count %;\
/test ++TFRA_Start %;\
/ELSE \
/test TFRA_Done := 1 %;\
/ENDIF %;\
/DONE %;\
/RETURN TFRA_Count
/def tfread_2array = \
/test TFRA2_Array := {1} %;\
/test TFRA2_Index := {2} %;\
/test TFRA2_Start := {3} %;\
/test TFRA2_Size := {4} %;\
/test TFRA2_File := {5} %;\
/test TFRA2_Count := 0 %;\
/test TFRA2_Done := 0 %;\
/test TFRA_Err := 0 %;\
/test TFRA2_St := '' %;\
/WHILE (TFRA2_Done = 0) \
/test TFRA2_Err := tfread(TFRA2_File, TFRA2_St) %;\
/IF ( (TFRA2_Err != -1) & (TFRA2_Count < TFRA2_Size) ) \
/test put_2array(TFRA2_Array, TFRA2_Index, TFRA2_Start, TFRA2_St) %;\
/test ++TFRA2_Count %;\
/test ++TFRA2_Start %;\
/ELSE \
/test TFRA2_Done := 1 %;\
/ENDIF %;\
/DONE %;\
/RETURN TFRA2_Count
;========================================================================
; strstr_array() / strstr_2array()
;
; Searches for a value in a virtual array and returns what element its found in.
; USAGE:
; x := strstr_array("array_name", start_index, size, value)
; x := strstr_2array("array_name", index, start_index, size, value)
;
; x : Element of "array_name" that value was found in.
; -1 is returned if value was not found.
; "array_name" : Name of the array
; start_index : Element to start searching at
; index : Start_index of index [first dimension] (strstr_2arrat only)
; size : Number of elements to search
; value : The item your searching for
;
; NOTES: Strstr_2array can't search all dimesions, you must search each dimension
; at a time.
; If value = "" then it will return the first element that is blank.
;========================================================================
/def strstr_array = \
/test SSA_Array := {1} %;\
/test SSA_Start := {2} %;\
/test SSA_Size := {3} %;\
/test SSA_Value := {4} %;\
/test SSA_Count := 0 %;\
/test SSA_Element := -1 %;\
/WHILE ( (++SSA_Count <= SSA_Size) & (SSA_Element = -1) ) \
/test SSA_St := get_array(SSA_Array, SSA_Start) %;\
/IF (SSA_Value =~ '') \
/IF (SSA_St =~ '') \
/test SSA_Element := SSA_Start %;\
/ENDIF %;\
/ELSE \
/test SSA_Pos := strstr(SSA_St, SSA_Value) %;\
/IF (SSA_Pos > -1) \
/test SSA_Element := SSA_Start %;\
/ENDIF %;\
/ENDIF %;\
/test ++SSA_Start %;\
/DONE %;\
/RETURN SSA_Element
/def strstr_2array = \
/test SSA2_Array := {1} %;\
/test SSA2_Index := {2} %;\
/test SSA2_Start := {3} %;\
/test SSA2_Size := {4} %;\
/test SSA2_Value := {5} %;\
/test SSA2_Count := 0 %;\
/test SSA2_Element := -1 %;\
/WHILE ( (++SSA2_Count <= SSA2_Size) & (SSA2_Element = -1) ) \
/test SSA2_St := get_2array(SSA2_Array, SSA2_Index, SSA2_Start) %;\
/IF (SSA2_Value =~ '') \
/IF (SSA2_St =~ '') \
/test SSA2_Element := SSA2_Start %;\
/ENDIF %;\
/ELSE \
/test SSA2_Pos := strstr(SSA2_St, SSA2_Value) %;\
/IF (SSA2_Pos > -1) \
/test SSA2_Element := SSA2_Start %;\
/ENDIF %;\
/ENDIF %;\
/test ++SSA2_Start %;\
/DONE %;\
/RETURN SSA2_Element
;========================================================================
; purge_array()
;
; Purges a virtual array made by get_array() and put_array()
; USAGE:
; purge_array("array_name")
;
; "array_name" : Name of array you want to delete from memory.
;
; NOTES: Purge_array() deletes the whole entire virtual array. It
; can be a double dimensioned array as well.
;========================================================================
/def purge_array = \
/quote -S /unset `/listvar -s _%1_array_*
;========================================================================
; list_array() / list_2array()
;
; List an array in an easy to read format.
; USAGE:
; list_array("array_name", startindex)
; list_2array("array_name", startindex1, startindex2)
; /list_array <array_name> <startindex>
; /list_2array <array_name> <startindex1> <startindex2>
;
; "array_name" : Name of the array you want to list. (function form only)
; startindex : Starting element you want to list from.
; startindex1 : Starting element of startindex2 you want to list from.
; startindex2 : Starting element of startindex1
;========================================================================
/def list_array = \
/def list_array2 = \
/test la_rawname := {1} %%;\
/test la_name := {2} %%;\
/test la_index := substr(la_rawname, strlen(la_name) + 8, 256) %%;\
/IF (la_index >= {3}) \
/test echo(strcat(la_name, '[', la_index, '] :=', get_array(la_name, la_index))) %%;\
/ENDIF %;\
/quote -S /test list_array2("`"/listvar -s _%1_array_*"", {1}, {2}) %;\
/undef list_array2
/def list_2array = \
/test la2_i2F := 0 %;\
/def list_2array2 = \
/test la2_rawname := {1} %%;\
/test la2_name := {2} %%;\
/test la2_rawindex := substr(la2_rawname, strlen(la2_name) + 8, 256) %%;\
/test la2_pos := strstr(la2_rawindex, '_') %%;\
/test la2_index1 := substr(la2_rawindex, 0, la2_pos) %%;\
/test la2_index2 := substr(la2_rawindex, la2_pos + 1, 255) %%;\
/IF (la2_index1 >= {3}) \
/IF ( (la2_i2F = 0) & (la2_index2 >= {4}) )\
/test la2_i2F := 1 %%;\
/ENDIF %%;\
/IF (la2_i2F = 1) \
/test echo(strcat(la2_name, '[', la2_index1, '][', la2_index2, '] :=', get_2array(la2_name, la2_index1, la2_index2))) %%;\
/ENDIF %%;\
/ENDIF %;\
/quote -S /test list_2array2("`"/listvar -s _%1_array_*"", {1}, {2}, {3}) %;\
/undef list_2array2
|