2012-12-21 45 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,因此它(有效地)作为过程调用的参数被“引用”传递。