在数组高速缓存系统间查找值

问题描述 投票:0回答:3

我希望有人可以帮助我试图找到一种方法来找到数组存在的值,如PHP in_array

我有两个变量PTOWNADDRESS。我想找出PTOWN是否在数组中的任何位置?

arrays intersystems-cache mumps
3个回答
0
投票

你在找if $data(ADDRESS("street 1")) write "found"吗?


0
投票

您在键或节点的值中寻找PTOWN吗?如果可以在键中找到PTOWN,则可以使用$ ORDER查找与您拥有的最接近的匹配项。假设你有一个像这样的数组:

new PTOWN,Address,Array
set PTOWN="Paris"
set Address="Some address"
;
set Array("Paris")="123 Avenue Louis Pasteur"
set Array("Madrid")="434 Calle De Duermos"
set Array("Boston")="1522 Market Street"
;
if $ORDER(Array(PTOWN))=PTOWN d
    ...

$ORDER将返回与您的搜索词最匹配的密钥,并且也会在MUMPS的订购系统中返回。所以$ORDER(Array("Pa"))回归“巴黎”,$ORDER(Array("Mad"))回归“马德里”,而$ORDER(Array("Alameda"))回归“波士顿”,因为“波士顿”是阵中“Alameda”之后的下一个东西。

您可以使用它来循环遍历数组中的所有键:

new nodeName
set nodeName="" ; you have to pass $ORDER "" to get the first thing in the array
;
for  set nodeName=$ORDER(Array(nodeName)) quit:nodeName=""  d 
    ; $ORDER returns "" when you pass it the *last* key in the array, so we can quit this loop when we get that back
    ;
    if nodeName=PTOWN write Array(nodeName)

如果在数组的值中找到PTOWN,则必须使用循环方法。


0
投票

假设PTOWN是'$ needle'并且ADDRESS是'$ haystack'(根据in_array文档),并且不知道数组格式,你可以做的最好的事情是这样的:

in_array(needle,haystack)
    NEW idx,found,subAry
    SET found=0 ; If you want to return 0 instead of "" if not found
    ;
    FOR $ORDER(haystack(idx)) QUIT:idx="" DO  QUIT:found
    . ; If the format of the array is: array(<index>)=<value>
    . IF haystack(idx)=needle SET found=1 QUIT
    . ;
    . ; If the format of the array is: array(<key>)=<value>
    . ; Note: this type of search can be made a bit more efficient, but we're brute-forcing here
    . IF idx=haystack SET found=1 QUIT
    . ;
    . ; If you're using nested keys and values...this is NOT recommended since Cache doesn't really support recursion
    . MERGE subAry=haystack(idx)
    . SET found=$$in_array(needle,subAry)
    ;
    QUIT found

你这样称呼它:

...=$$in_array(PTOWN,.ADDRESS) ; Don't forget to pass by reference!
© www.soinside.com 2019 - 2024. All rights reserved.