I would like to post here a script that workarounds the lack of support of arrays in ScriptFTP (as of version 3.3 arrays are not supported). Bucket, a power forum user, sent me it by email. Thanks!
#|keyvalue|key2value2
#or for k1=12 and k2=5 and k3=34
#|k112|k25|k334
#
# Sample array:
$SourceList = "|k112|k25|k334"
# init list parsing logic
$newSourceList = ""
$isSource = 0
$keyLen = Textlength($key)
FOREACH $aSource in $SourceList
#does the key exist?
IF(Textcut($aSource,1,$keyLen) == $key )
$oldCount = Textcut($aSource,$keyLen + 1,-1)
$oldCount = $oldCount + $newCount
$newSourceList = $newSourceList."|".$key.$oldCount
$isSource = 1
ELSE
#wrong key, so just append whatever it is that we have
$newSourceList = $newSourceList."|".$aSource
END IF
END FOREACH
# if we did not find the key, then we must add it here for next time
IF ($isSource == 0 AND $newCount > 0)
$newSourceList = $newSourceList."|".$key.$newCount
endif
# ready for next time
$SourceList = $newSourceList[/ScriptFTP]
I wanted to add a separate count for each camera I am tracking (Store and Forward). Because I use a generic "process" routine I could not just use a simple variable. Ideally I should use an array.[scriptftp="arrays.ftp"]# Basically the list will look like:
So I found a way to make an associative array...
#|keyvalue|key2value2
#or for k1=12 and k2=5 and k3=34
#|k112|k25|k334
#
# Sample array:
$SourceList = "|k112|k25|k334"
# init list parsing logic
$newSourceList = ""
$isSource = 0
$keyLen = Textlength($key)
FOREACH $aSource in $SourceList
#does the key exist?
IF(Textcut($aSource,1,$keyLen) == $key )
$oldCount = Textcut($aSource,$keyLen + 1,-1)
$oldCount = $oldCount + $newCount
$newSourceList = $newSourceList."|".$key.$oldCount
$isSource = 1
ELSE
#wrong key, so just append whatever it is that we have
$newSourceList = $newSourceList."|".$aSource
END IF
END FOREACH
# if we did not find the key, then we must add it here for next time
IF ($isSource == 0 AND $newCount > 0)
$newSourceList = $newSourceList."|".$key.$newCount
endif
# ready for next time
$SourceList = $newSourceList[/ScriptFTP]
The script pulls out the key, checks to see if it is the key I want to update, and if it is, it grabs the value, increments it and then puts it back into the list. The key can be any length. The value I am using is numeric, but you can put a text value (you just can't perform math on it).
The downside is that you do not have functions (or GOSUB ) so you need to copy the code everywhere you want to access or update the array. Or else build a return block.
Anyway, I gift this code snippet to you to do with as you wish.
liked Script FTP? Try our other product Email Parser