回复:列表中每個項目的任意操作(功能等)如果您的情況是這樣,那麼您至少可以通過使用 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]);回复:列表中每個項目的任意操作(功能等)多謝你們!
這太棒了。
我在我們的內部網中找到了JMP16,IT部門說他們正在準備安裝說明並正式發布。 所以我還是繼續安裝了它。
我的實際任務是(曾經)有點複雜,例如,在一個例子中,我有一個類型的字符串列表
"120, 130, 160, 120, 300, 200"
實際數據將是一列,每一行都具有該類型的字符串(僅長得多)
我需要把它變成數字列表的列表。 所以我需要將該列讀入一個初始列表,然後在這個初始列表上應用 Words() 以獲得字符串列表列表,然後應用 Num() 將每個字符串轉化為一個數字。 這意味著兩個嵌套循環。
現在我已經用兩個 For Each 循環完成了該操作,但讓我研究一下您在此處發布的建議。 我永遠無法理解 Expr() - 我想我需要再給它一次機會。
謝謝!
回复:列表中每個項目的任意操作(功能等)@miguello ,
@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
回复:列表中每個項目的任意操作(功能等)正如您已經註意到的那樣,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);
對列表中每個項目的任意操作(函數等)是否有一種更簡單的方法可以對列表中的每個項目應用相同的函數,而不是迭代列表?
例如:
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
消費者和市場研究實驗設計質量和工藝工程