取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Discussions

Solve problems, and share tips and tricks with other JMP users.
选择语言 隐藏翻译栏
lala
Level IX

如何用JSL将没有分行的文本按字节数拆分为多行的表格?

请教专家:

如何用脚本将下面的文本、按每 64 个字符拆分为1行、

598A0F0548917B44F6C87B44855B7B44D7637B44DA57BE45140B000000000000598A1405715D7B4433637B445CFF7A441F057B4460562A45DE04000000000000598A1905F6087B44C3357B4433F37A44A4007B44C07519455304000000000000598A1E055CFF7A44C3257B445CCF7A44F6D87A4460D10845D903000000000000

得到这样的表格

2021-09-01_114831.png

 

1 个已接受解答

已接受的解答
jthi
Super User

Re: 如何用JSL将没有分行的文本按字节数拆分为多行的表格?

Many was to do this and it depends a little bit how your strings look like (do they always have that long sequence of zeroes, is it always dividable with 64, how long it is).

 

Below is one maybe not so common way to do it by using Regex Match:

Names Default To Here(1);
txt = "598A0F0548917B44F6C87B44855B7B44D7637B44DA57BE45140B000000000000598A1405715D7B4433637B445CFF7A441F057B4460562A45DE04000000000000598A1905F6087B44C3357B4433F37A44A4007B44C07519455304000000000000598A1E055CFF7A44C3257B445CCF7A44F6D87A4460D10845D903000000000000";
splitCount = Length(txt) / 64; //amount of splits
regexPattern = repeat("(.{64})", splitCount); //create regex pattern to be used with Regex Match()
regexList = Regex Match(txt, regexPattern); //get list of splits
answer = Substr(regexList, 2); //drop first index, because it has the whole string (see scripting index for why)

//add to table
dt = New Table("test",
	New Column("test", Character, Values(answer)),
	New Column("lenght", Numeric, Continuous, << Set Each Value(Length(:test)))
);

You could also use For loop and Substr, insert extra values to string and use Words after that

 

Edit:

Other regex method:

Names Default To Here(1);

txtToSplit = "598A0F0548917B44F6C87B44855B7B44D7637B44DA57BE45140B000000000000598A1405715D7B4433637B445CFF7A441F057B4460562A45DE04000000000000598A1905F6087B44C3357B4433F37A44A4007B44C07519455304000000000000598A1E055CFF7A44C3257B445CCF7A44F6D87A4460D10845D903000000000000";
splitLength = "64";
regexPattern = "([^\n]{1,"||splitLength||"})";
replaceChar = ".";
result = Words(Regex(txtToSplit, regexPattern, "\1"||replaceChar,GLOBALREPLACE), replaceChar);

With For loop and Substr:

Names Default To Here(1);

txtToSplit = "598A0F0548917B44F6C87B44855B7B44D7637B44DA57BE45140B000000000000598A1405715D7B4433637B445CFF7A441F057B4460562A45DE04000000000000598A1905F6087B44C3357B4433F37A44A4007B44C07519455304000000000000598A1E055CFF7A44C3257B445CCF7A44F6D87A4460D10845D903000000000000";
splitCount = Length(txtToSplit) / 64; //amount of splits
resultList = {};

For(i = 0, i < splitCount, i++,
	Insert Into(resultList, Substr(txtToSplit, 1+i*64, 64));
);
show(resultList);

-Jarmo

在原帖中查看解决方案

1 条回复1
jthi
Super User

Re: 如何用JSL将没有分行的文本按字节数拆分为多行的表格?

Many was to do this and it depends a little bit how your strings look like (do they always have that long sequence of zeroes, is it always dividable with 64, how long it is).

 

Below is one maybe not so common way to do it by using Regex Match:

Names Default To Here(1);
txt = "598A0F0548917B44F6C87B44855B7B44D7637B44DA57BE45140B000000000000598A1405715D7B4433637B445CFF7A441F057B4460562A45DE04000000000000598A1905F6087B44C3357B4433F37A44A4007B44C07519455304000000000000598A1E055CFF7A44C3257B445CCF7A44F6D87A4460D10845D903000000000000";
splitCount = Length(txt) / 64; //amount of splits
regexPattern = repeat("(.{64})", splitCount); //create regex pattern to be used with Regex Match()
regexList = Regex Match(txt, regexPattern); //get list of splits
answer = Substr(regexList, 2); //drop first index, because it has the whole string (see scripting index for why)

//add to table
dt = New Table("test",
	New Column("test", Character, Values(answer)),
	New Column("lenght", Numeric, Continuous, << Set Each Value(Length(:test)))
);

You could also use For loop and Substr, insert extra values to string and use Words after that

 

Edit:

Other regex method:

Names Default To Here(1);

txtToSplit = "598A0F0548917B44F6C87B44855B7B44D7637B44DA57BE45140B000000000000598A1405715D7B4433637B445CFF7A441F057B4460562A45DE04000000000000598A1905F6087B44C3357B4433F37A44A4007B44C07519455304000000000000598A1E055CFF7A44C3257B445CCF7A44F6D87A4460D10845D903000000000000";
splitLength = "64";
regexPattern = "([^\n]{1,"||splitLength||"})";
replaceChar = ".";
result = Words(Regex(txtToSplit, regexPattern, "\1"||replaceChar,GLOBALREPLACE), replaceChar);

With For loop and Substr:

Names Default To Here(1);

txtToSplit = "598A0F0548917B44F6C87B44855B7B44D7637B44DA57BE45140B000000000000598A1405715D7B4433637B445CFF7A441F057B4460562A45DE04000000000000598A1905F6087B44C3357B4433F37A44A4007B44C07519455304000000000000598A1E055CFF7A44C3257B445CCF7A44F6D87A4460D10845D903000000000000";
splitCount = Length(txtToSplit) / 64; //amount of splits
resultList = {};

For(i = 0, i < splitCount, i++,
	Insert Into(resultList, Substr(txtToSplit, 1+i*64, 64));
);
show(resultList);

-Jarmo

推荐文章