not meaning to hi-jack your post but here's my method of using arrays, supports multi-dimensional arrays, and multi-type keys(int or string)
alias array {
/*
* Start of data parsing
* Do not edit
*/
.noop $regex(prop,$prop,/^(\w+)(\x28(.+)?\x29)?/)
var %this = $+(ARRAY.,$$1),%prop = $iif($regml(prop,1),$regml(prop,1),init),%param = $iif($regml(prop,3),$v1,$null),%addData = .hadd %this
if (%param) { .tokenize 44 %param }
if (!$hget(%this) && %prop != init) { echo -a * Error: Object not initialized | halt }
if ($isalias($+(%this,.,%prop,.PRIVATE))) { echo -a * ERROR: Unable to access Method $qt(%prop) | halt }
if ($regml(prop,0) == 1) { $iif($1,.hadd %this %prop $1,return $hget(%this,$prop)) | halt }
$iif($isalias($+(%this,.,%prop)),noop $+($,%this,.,%prop,$chr(40),%prop,$chr(44),$1-,$chr(41)),.goto %prop)
halt | ; prevent script from passing this point
/*
* Class methods
* Start editing here
*/
:init
if ($hget(%this)) {
echo -a * Error: Array already exists
halt
}
.hmake %this 10
return 1
:add
if (!$3) {
echo -a * ERROR: insufficient parameters
halt
}
var %index = $replace($($+($,2-,$calc($0 -1)),2),$chr(32),$chr(243)),%data = $($+($,$0),2)
.hadd %this %index %data
return 1
:get
if (!$2) {
echo -a * ERROR: insufficient parameters
halt
}
var %index = $replace($($+($,2-,$0),2),$chr(32),$chr(243))
echo -a $iif($hget(%this,%index),$v1,$null)
return 1
:destroy
if ($hget(%this) && $2 == 1) { .hfree %this }
return 1
:error
echo -a $iif($error,$v1,Unknown error) in Class: $qt(%this)
.reseterror
return 0
}
syntax:
$array() ; creates the array
$array(,index,data).set() ; sets data
$array(,index).get() ; retrieves data
examples:
$array(test)
$array(test,1,this is stored in index 1).set()
$array(test,2,1,this is stored in index 2[1]).set()
$array(test,1).get() returns "this is stored in index 1"
$array(test,2,1).get() returns "this is stored in index 2[1]"
$array(test,4).get() returns $null