목록의 각 항목에 대한 임의 작업(함수 등)목록을 반복하는 대신 목록의 각 항목에 동일한 기능을 적용하는 더 간단한 방법이 있습니까?
예를 들어:
stringlist = {"1", "2"};
numericlist = {};
for (i = 1, i<=N Items(stringlist), i++,
numericlist[i] = Num(stringlist[i])
);
show(numericlist);다음과 같이 더 간단한 것이 있습니까?
numericlist = Num(stringlist);
그러나 이것은 분명히 작동하지 않습니다. 다른 방법이 있나요?
추신 JMP 16의 새로운 For Each 기능을 발견했습니다. 불행히도 저는 여전히 JMP 15를 사용 중입니다.
스크립팅Re: 목록의 각 항목에 대한 임의 작업(함수 등)귀하의 경우가 이와 같으면 Parse()를 사용하여 최소한 변환에 대한 반복을 피할 수 있습니다(문자열을 반복하는 것이 더 빠를 수 있음).
Names Default To Here(1);<BR />str = "120, 130, 160, 120, 300, 200";
numList = Parse("{" || str ||"}");
Show(numList[1], numList[2]);데이터 테이블로:
Names Default To Here(1);
dt = New Table("Untitled",
Add Rows(12),
Compress File When Saved(1),
New Column("Column 1",
Character,
"Nominal",
Set Values(
{"120, 130, 160, 120, 300, 200", "120, 130, 160, 120, 300, 200",
"120, 130, 160, 120, 300, 200", "120, 130, 160, 120, 300, 200",
"120, 130, 160, 120, 300, 200", "120, 130, 160, 120, 300, 200",
"120, 130, 160, 120, 300, 200", "120, 130, 160, 120, 300, 200",
"120, 130, 160, 120, 300, 200", "120, 130, 160, 120, 300, 200",
"120, 130, 160, 120, 300, 200", "120, 130, 160, 120, 300, 200"}
)
)
);
dt << New Column("numlist", Expression, << set each value(Parse("{" || :Column 1|| "}")));
numListList = dt:numList << get as matrix;
Show(numListList, numListList[1], numListList[1][1]);Re: 목록의 각 항목에 대한 임의 작업(함수 등)고마워, 얘들아!
이것은 굉장합니다.
실제로 인트라넷에서 JMP16을 찾았습니다. IT 부서에서는 설치 지침을 준비하고 공식적으로 발표하는 과정에 있다고 말했습니다. 그래서 어쨌든 설치하고 진행했습니다.
내 실제 작업은 조금 더 복잡했습니다. 예를 들어, 한 경우에는 다음 유형의 문자열 목록이 있었습니다.
"120, 130, 160, 120, 300, 200"
실제 데이터는 해당 유형의 문자열이 있는 각 행이 있는 열입니다(훨씬 더 깁니다).
나는 그것을 숫자 목록으로 바꿔야했습니다. 그래서 그 열을 초기 목록으로 읽은 다음 이 초기 목록에 Words()를 적용하여 문자열 목록 목록을 가져온 다음 Num()을 적용하여 각 문자열을 숫자로 가져와야 했습니다. 이것은 두 개의 중첩 루프를 의미합니다.
지금은 For Each 루프 2개로 이 작업을 수행했지만 여기에 게시한 제안 사항을 살펴보겠습니다. 나는 Expr()을 결코 이해할 수 없었다 - 나는 그것에 또 다른 기회를 주어야 한다고 생각한다.
감사!
Re: 목록의 각 항목에 대한 임의 작업(함수 등)@미구엘로 ,
@jthi 당신에게 훌륭한 답변을 주었습니다(감사합니다).저 역시 JMP 16의 TransformEach 기능에 매우 흥분했습니다.
다음은 훨씬 이전 버전의 JMP용으로 작성되고 JMP13용으로 게시된 스크립트의 수정된 부분입니다. 제 이전 동료 중 많은 사람들이 LFUNC 함수를 사용했습니다(이름은 목록 함수의 약어입니다).JMP 16 이름 "TransformEach"와 구문이 너무 좋습니다. 그러나 목록 항목 중 하나가 지정된 변환에 대해 잘못된 유형인 경우 오류가 반환됩니다. Try() 함수는 이를 수정할 수 있습니다.
JMP 16에 도달할 때까지 이것이 유용할 수 있는 경우를 대비하여 게시합니다.코드의 함수 정의 부분을 파일에 저장하고 필요한 스크립트에서 Include()를 사용하기만 하면 됩니다.
// JSL Companion: Applications of the JMP Scripting Language
// Title: JSLC_CustomListFunctions.jsl
// Version: Windows JMP® 13.2.0
// Purpose: Provides examples of writing user functions.
/*Notes:
These functions are provided for learning as well as utility.
See Chapter 5 and 5_Extra_CustomListFunctions.jsl");
The script will define 2 functions. After each is a comment
block /*: Usage & Test examples ... --- end Test*/. Remove
The comment block and run the examples one line at a time. Open
the embedded log or have the log window visible to see
the results.
Motivation:
As seen in Chapter 5, a numeric function on a single layer
list of numbers, or a vector, or a matrix, will return the
same object with the function applied to each item. For example,
xx = Sqrt({1,2,3,4,9}); //xx = {1, 1.4142135623731, 1.73205080756888, 2, 3}
We have suggested that the same apply to String functions (that operate
on a single item) on a list of text. What we would like is
xx = Substr({"Quaker", "Minnesota", "Nebraska"}, 1,1); would return
xx = {"Q", "M", "N"}. However, currently most JMP String functions do not
allow list agruments.
Functions defined here, are:
- LFunc( mylist, expr); where expression is like expr(Substr(x,1,1)).
{LIKE function notes removed}
LFunc can save writing for-loops, Like save looking up the REGEX syntax.
*/
//------LFunc-------------------------------------------------------------------
Lfunc = Function( {xList, fExpr/*ex. Expr(Contains(x,"h"))*/},
{x = {}, i = 1, errCode = 0, errStr = "\!NError bad argument", newExpr},
If( !Is List( xList ),
errCode = 1
);
If(
Type( Name Expr( fExpr ) ) == "Name",
//if Name then JMP does not recognize the function
tmp = Trim( Char( Name Expr( fExpr ) ) );
//extract the function and check if it is contained in the global namespace, if not error!
If( Namespace( "global" ) << Contains( Word( 1, tmp, ":(" ) ) == 0,
errCode = errCode + 2
);,
Type( Name Expr( fExpr ) ) != "Expression", errCode = errCode + 2
);
//catches typos, returns Name if not a valid function
//catches typos, returns Name if not a valid function
If( !Is Expr( Name Expr( fExpr ) ),
errCode = errCode + 2
); //catches syntax
If(
!errCode,
For( i = 1, i <= N Items( xList ), i++,
newExpr = Substitute( Name Expr( fExpr ), Expr( x ), xList[i] );
Insert Into( x, Try( newExpr, Empty() ) );
),
errCode == 1, Write( errStr || "(1) not a list \!N" ),
errCode == 2, Write( errStr || "(2) not a valid expression \!N" ),
errCode == 3, Write( errStr || "s (1) is not a list and (2) is not a valid expression \!N" )
);
x//return x
;
); //End Lfunc
//------------------------------------------------------------------------------
//Usage & Test ...remove the slash asterisk and run in segments, results are in the log
//------------------------------------------------------------------------------
myList1={"john", "harry", "huh","ah ha", "xyx", , "a", "h", 7, [1,7]};
/* example below returns a list of numeric values, one value for each item in the list
n =>1st location of h, 0 => does not contain "h", Empty() => the item is not
a valid argument for that expression.
*/
zz1 = Lfunc(myList1,Expr(Contains(x,"h")));
show(zz1);
myList2={"john", "harry", "huh","ah ha", "xyx", , "a", "h"};
zz2=Lfunc(myList2, Expr(Uppercase(x)));
show(zz2);
//-----------------------------------------------------------------------------------------
/* returns a list of how many words are in each string
[] -> no occurrences
Empty() => the expression is not valid for that item.
*/
zz3 = Lfunc(mylist2, expr(nitems(words(x))));
show(zz3);
//-----------------------------------------------------------------------------------------
/* This specified function requires numeric arguments
Empty() => the expression is not valid for that item.
A numeric function applied to a matrix returns a matrix
of the results applied on each element in the matrix.
Only the last 2 items are valid arguments for Log()
*/
zz4 = Lfunc(mylist1, expr(log(x)));
show(zz4);
//----------------Error Checking---------------------------------------------------------------
/* Uncomment the code below to show the error capture and reporting */
//zz = Lfunc("oh johhny", Expr(Contains(x,"h")) ); //not a list
//zz = Lfunc({"oh johhny"}, Expr(Contians(x,"h")) ); //type not a valid expr due to typo
//zz = Lfunc("oh johhny", Expr(Contians(x,"h")) ); //not a list and not a valid expr
Re: 목록의 각 항목에 대한 임의 작업(함수 등)이미 알고 있듯이 JMP16은 For Each 및 Transform Each와 함께 이 문제를 해결하는 데 도움이 됩니다. 이 작업을 여러 번 수행해야 하는 경우 작업을 수행하기 위해 사용자 지정 함수를 만들 수 있습니까?
Names Default To Here(1);
charToNum = function({tempList}, {Default Local},
newList = {};
For(i = 1, i <= N Items(tempList), i++,
newList[i] = Num(tempList[i]);
);
return(newList);
);
a = {"1", "2"};
b = charToNum(a);
show(b);
편집하다:
다른 변환을 처리할 수 있는 더 복잡한 함수를 만들 수도 있습니다. 대부분 과도할 것이며 권장하지 않을 것입니다(다른 변환에 대해 다른 기능을 생성할 뿐입니다).
다음은 수행할 수 있는 방법의 한 예입니다.
Names Default To Here(1);
transformList = function({tempList, transformExpression}, {Default Local},
newList = {};
For(i = 1, i <= N Items(tempList), i++,
val = tempList[i];
newList[i] = transformExpression
);
return(newList);
);
a = {"1", "2", "100"};
b = transformList(a, Expr(Num(val)));
c = transformList(b, Expr(Multiply(val, 10)));
d = transformList(c, Expr(Char(val)));
Show(a,b,c,d);
//can also use custom functions with transformList
padZero = function({tempValue}, {Default Local},
if(Length(tempValue) < 2,
tempValue = "0" || tempValue;
);
return(tempValue)
);
e = transformList(a, Expr(padZero(val)));
Show(e);
f = transformList(e, Expr(Length(val)));
Show(f);