你好,
我正在努力解決跨多個關聯數組的 For Each 循環的語法。
例如:
q = ["q1"=>11, "q2"=>22, "q3"=>33];
w = ["w1"=>1111, "w2"=>2222, "w3"=>3333];
對於單一數組,僅供參考:
For Each( {a, i}, q, Show( i, a ) ); // retrieves indices and keys
For Each( { {a, b}, i }, q, Show( i, a, b ) ); // retrieves indices, keys and values類似地,它在某種程度上適用於兩個陣列:
For Each( {a, i}, Across( q, w ), show( i, a ) );<BR />// retrieves indices and two-item lists of keys of both arrays:
i = 1;
a = {"q1", "w1"};
i = 2;
a = {"q2", "w2"};
i = 3;
a = {"q3", "w3"};然後進一步鑽探:
For Each( { {a, b}, i }, Across( q, w ), show( i, a, b ) );<BR />// retrieves indices and keys of both arrays separately:
i = 1;
a = "q1";
b = "w1";
i = 2;
a = "q2";
b = "w2";
i = 3;
a = "q3";
b = "w3";但是,我也想獲得價值觀。我嘗試了幾種大括號分組的變體,但沒有任何效果。從單一數組語法來看,這應該是最明顯的一個:
For Each( { { {a, b}, {c, d} }, i }, Across( q, w ), show( i, a, b, c, d ) );<BR />// returns error:<BR />argument value is invalid in access or evaluation of 'For Each' , Bad Argument( {a, b} ), For Each/*###*/({{{a, b}, <BR />{c, d}}, i}, Across( q, w ), Show( i, a, b, c, d ))我進行了 RTFM 和 RTFF 編輯,但找不到任何內容。
我當然可以這樣做:
For Each( { {a, b}, i }, Across( q, w ), show( i, a, b, q[a], w[b] ) );但這不是重點。這是一個優雅的問題。
有任何想法嗎?
謝謝回覆。如前所述,並不是我無法輕鬆提取這些值。我只是覺得奇怪的是,對於單一關聯數組,也可以使用嵌套列表引用值,但不能跨多個數組引用值。
https://www.jmp.com/support/help/en/17.2/#page/jmp/conditional-and-logic-functions.shtml?os=win&source=application#ww10169967
使用Across()關鍵字指定多個容器時,名稱清單包含引用每個容器中的值的名稱清單。名稱清單中第一項的名稱數量必須與Across()關鍵字中指定的容器數量相符。
對於關聯數組
對於關聯數組容器,名稱清單包含兩個項目的名稱清單以及關聯數組中每個項目的索引名稱。
所以我認為你必須按照你的建議去做
Names Default To Here(1);
aa1 = ["q1" => 11, "q2" => 22, "q3" => 33];
aa2 = ["w1" => 1111, "w2" => 2222, "w3" => 3333];
For Each({{key1, key2}, i}, Across(aa1, aa2),
val1 = aa1[key1];
val2 = aa2[key2];
Print(Eval Insert("\!Ni = ^i^"));
Print(Eval Insert("key1 = ^key1^, val1 = ^val1^"));
Print(Eval Insert("key2 = ^key2^, val2 = ^val2^"));
);
或者也許是這樣的(這也感覺有點奇怪)
Names Default To Here(1);
aa1 = ["q1" => 11, "q2" => 22, "q3" => 33];
aa2 = ["w1" => 1111, "w2" => 2222, "w3" => 3333];
For Each({{key1, key2, val1, val2}, i}, Across(aa1, aa2, aa1 << get values, aa2 << get values),
Print(Eval Insert("\!Ni = ^i^"));
Print(Eval Insert("key1 = ^key1^, val1 = ^val1^"));
Print(Eval Insert("key2 = ^key2^, val2 = ^val2^"));
);
可能應該使用一些語法,但我不知道它可能是什麼,我同意這樣的語法很可能是最明顯的語法
For Each({{{key1, val1}, {key2, val2}}, i}, Across(aa1, aa2), Show(i, key1, val1, key2, val2));