使用帶空While條件不起作用的For循環遍歷關聯數組我的建議主要來自https://community.jmp.com/t5/JSL-Cookbook/Looping-through-an-Associative-Array-s-elements/tac-p/381693#M112關於如何遍歷關聯數組的信息。 我的理解是,迭代器遍歷鍵,而不是值,但是您可以輕鬆訪問這些值,從而得到數組。
我的代碼如下。
Names Default To Here(1);
dt1 = Open("file path" );
dt2 = Open("file path 2");
Summarize(dt1, IDs = by(:ID)); //this finds all the unique IDs in the data file, which we will need
//First, populate the temporary data structure--associative array--from the dt1 data file which we will then copy over to the dt2 data file.
For (i=1, i<=N Items(IDs), i++,
ID = IDs[i];
temp_array = Associative Array ({"Val1","Val2","Val3"},{{},{},{}});
For ( temp = temp_array << First, Is Empty(temp_array[temp]), temp = temp_array << Next(temp),//looping through temp_array's keys is clunky but done this way in JMP
query_date = dt1 << Select Where (:"ID" == ID);
Write(query_date);
//etc not there yet because the above not working
);
);
在我的旅程中,此刻感興趣的變量是query_date,我需要繼續進行操作。問題是,就其編寫而言,由於某種原因,內部循環甚至無法運行一次。 循環中的while條件應評估為True,直到我希望循環終止,所以我將其設置為
Is Empty(temp_array[temp])
因為temp_array [temp]是關聯數組中的第一個值,其中temp是第一個鍵“ Val1”,並且值是{}。 我猜一個重要的問題是:在Is Empty()中{}的計算結果為True還是False? 我什至無法在Google上搜索,因為{}是Google無法處理的特殊字符。 這樣您就能明白為什麼我感到困惑。
我已經將腳本的其餘部分進行了編碼,並且我非常有信心在這一點上可以使用較小的錯誤,因此,如果我能夠克服這個障礙,將會大有幫助。 在我的腦海中,偽代碼非常清楚,作為JMP JSL的新用戶,情況越來越清晰了,對此我深表感謝。 謝謝大家的幫助!
麥克風
數據表腳本編寫回复:使用帶有空而條件不起作用的For循環遍歷關聯數組需要在密鑰不為空的情況下運行for循環的條件
For ( temp = temp_array << First, ! Is Empty(temp), temp = temp_array << Next(temp),
當不再有鍵時,關聯數組的<<<和<< next方法要么返回鍵(字符串或數字),要么返回特殊的empty()值。 該鍵(如果不為空)可以用作關聯數組的索引。
這 !是個不是操作員。
編輯:您可能想要這樣的事情
temp_array = Associative Array( {"Val1", "Val2", "Val3"}, {{1, 2}, {}, {3, 4, 5}} );
For( temp = temp_array << First, !Is Empty( temp ), temp = temp_array << Next( temp ),
If( N Items( temp_array[temp] ) > 0,
Write( "\!nkey=", temp, " has ", N Items( temp_array[temp] ), " elements" ),
Write( "\!nkey=", temp, " has no elements" )
)
);key = Val1有2個元素
key = Val2沒有元素
key = Val3有3個元素
回复:使用帶有空而條件不起作用的For循環遍歷關聯數組我不明白為什麼您要使用關聯數組?您可以擴展您想執行的循環的設想嗎?
IsEmpty()是一個函數,如果腳本不知道所引用的項目是什麼,則返回1;如果在執行IsEmpty()之前在腳本中使用了所引用的項目,則返回0。
所以:
a={};
isEmpty(s);
// returns 0 since a has been referenced prior in the script
isEmpty(b);
// returns 1 since JMP has not seen the variable b before
關於
query_date = dt1 << Select Where (:"ID" == ID);
Select Where()執行一個動作,但不返回任何內容。因此query_date沒有值。Select Where()的作用是將數據表中符合選擇條件的所有行的“選定行狀態”設置為“打開”。如果數據表中多於一行的ID值,則將選擇多行。也許您正在尋找的是所選行的查詢日期的值。您可以獲取所選行的行號
dt1 << select where( :ID == ID );
theRows = dt1 << get selected rows;
它返回行號矩陣,而不是定標器。因此,要獲取query_date,必須引用下標。
dt1 << select where( :ID == ID );
theRows = dt1 << get selected rows;
query_date = dt1:query_date[ theRows[1] ];
一種更簡單的形式(不需要選擇步驟)是使用Get Rows Where()。
query_date = dt1:query_date[ dt1 << get rows where( :ID == ID )[1] ];
希望我能有所幫助,不要讓我感到困惑。請繼續問您的問題。
回复:使用帶有空而條件不起作用的For循環遍歷關聯數組謝謝你,吉姆。 很快的回复,對您來說真是太好了。 我很感激。 給我一點時間再次解析所有這些,這是周末。
麥克風回复:使用帶有空而條件不起作用的For循環遍歷關聯數組吉姆,謝謝您的答复。 一件事:我不明白
query_date = dt1:query_date[ theRows[1] ];
因為query_date尚未實例化。
這是拼寫錯誤(應該是其他變量)嗎?
日誌還會在此處引發錯誤:
Scoped data table access requires a data table column or variable in access or evaluation of 'dt_ld4:query_date' , dt_ld4:query_date/*###*/
我通常會自己解決這個問題,但是事實證明這個問題令人驚訝,因此我不確定。
謝謝一堆
麥克風
回复:使用帶有空而條件不起作用的For循環遍歷關聯數組嗨,Craige。 這似乎有效。 非常感謝你。
麥可
回复:使用帶有空而條件不起作用的For循環遍歷關聯數組好吧...我在猜。您指出需要找到query_date,並且看來它與您的Select Where()有關。因此,我猜您的數據表中有一列稱為query_date,並且Select Where()或Get Rows Where()將返回具有所需查詢日期的行。而且顯然我猜錯了在哪裡可以找到查詢日期。
回复:使用帶有空而條件不起作用的For循環遍歷關聯數組我在跑
dt1 << select where( :ID == ID );
theRows = dt1 << get selected rows;
Write(theRows);
只是為了檢查選擇theRows是否有效,但是看起來我們甚至還沒走得那麼遠。 它打印:
[](0, 1)
對於整個循環中的每次迭代,它應該從數據表中打印非常全面的一行(最多20列)。 我不知道發生了什麼。
為了嘗試回答您的隱式問題,theRows只是一個臨時變量,用於查找與循環中指定的值匹配的ID列值所導致的行(由於ID的多個副本,因此可能有多行具有這種性質)。
我意識到這很令人困惑...但這也是我也在努力的原因。
提前致謝,
麥克風