2012-12-21 82 views

回答

1

您可能只想將通常的索引編入table。請注意,以前未分配的名稱Y僅因分配給某個索引條目Y[a]而成爲table

restart: 
Y[a]:=211: 

Y[a]; 
          211 

Y[b]; 
          Y[b] 

assigned(Y[a]); 
          true 

assigned(Y[b]); 
         false 

eval(Y); 
         table([a = 211]) 

說了這麼多,它只是包裹拿到這裏訪問Y[b]回報NULLY[b]尚未分配你所描述的功能,一個小一點,等

您可以自定義的(或刪除)keysymbol的錯誤檢查和限制,因爲Maple允許更一般地對table進行索引。

restart: 

addKeyValuePair:=proc(containerRef::{indexed,symbol}, L::list) 
    if nops(L) <> 2 then 
    error "expecting list with two entries"; end if; 
    if not type(L[1], symbol) then 
    error "expecting list with symbol as first entry"; end if; 
    containerRef[L[1]]:=L[2]; 
    NULL; 
end proc: 

searchByKey:=proc(containerRef::{indexed,symbol}, key::name) 
    if assigned(containerRef[key]) then 
    containerRef[key]; 
    else 
    NULL; 
    end if; 
end proc: 

addKeyValuePair(Y, [a, 4.5]); 

searchByKey(Y, a); 
          4.5 

searchByKey(Y, b); 

searchByKey(Q, b); 

addKeyValuePair(Y, [a, 211]); 

searchByKey(Y, a); 
          211 

addKeyValuePair(Y, [c]); 
Error, (in addKeyValuePair) expecting list with two entries 

addKeyValuePair(Y, [2.34, 211]); 
Error, (in addKeyValuePair) expecting list with symbol as first entry 

A table是一種可變數據結構。它的類型爲last_name_eval,因此它(有效地)作爲過程調用的參數被「引用」傳遞。