使用带空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()中{}的计算结果是对还是错? 我什至无法在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的多个副本,因此可能有多行具有这种性质)。
我意识到这很令人困惑...但这也是我也在努力的原因。
提前致谢,
麦克风