您的位置:首页 > 其它

通过实例学习Virtools脚本语言VSL - 解析字符串

2009-07-31 18:36 591 查看
该习题演示解析字符串及用字符串中包含的信息填充数组(Array)。

开始一个新的作品并创建一个数组(Array)。把数组重命名为 "Players"
(没有引号) 并添加三个列(column),如下命名 - 列类型:
NickNames - String

Age - Integer

Score - Integer.

在Level下创建新脚本,并添加一个Run VSL BB。在VSL Script Manager中添加两个pIn。第一个pIn重命名为"data",类型设为String。第二个pIn重命名为"array",类型设为Array。

切换到 Schematic工作区,输入以下字符(不包括引号),作为“data”pIn的值:

"Eva,22,1024.

Jane,34, 544.

Pierre, 17, 5410.

John, 85,10."

你可能想要展开'data' pIn中的数据输入的字段。

构想是解析输入的字符串,提取出其中的信息,然后复制到数组中。该习题中,所需要的信息是名字、年龄和积分。逗号和句号作为数据是引不起人们兴趣的,但作为隔离数据字段或标志行结束点的字符是非常有用的。你会用到VSL <-
SDK 对应表 - 类与方法
中列出了的StringTokenizer类。给定要解析的字符串及用到的分隔符,"NextToken(str iPrevToken)" 这个方法就会一个个的提取出令牌。

【译注:网络资源 -

bruce

- 在邱仲潘译的《Mastering Java 2》有这么一段

 StreamTokenizer类根据用户定义的规则,从输入流中提取可识别的子串和标记符号,这个过程称为令牌化
(tokenizing
),因为流简化为了令牌符号。令牌
(token
)通常代表关键字、变量名、字符串、直接量
和大括号等语法标点。

我们参考邱仲潘的这段译文,统一为

token:令牌

tokenizing:令牌化

tokenizer:令牌解析器

cherami提到的翻译为“标记”,也可以理解,但token更准确的指一个字串(或流)中的以空格、','等(用户指定的规则)分割开来的一个一个的子串,使用“标记”好像范围比较窄。借用令牌网中的这个术语--“令牌”,我觉得很形象。



在代码窗口中输入下面的代码:
void main()

{
// We clear all data in the array

array.Clear();

// We create the first tokenizer in order to

// get data line by line. The "." separates lines.

str tokenLine = null;

StringTokenizer tokenizerLine(data.CStr(), ".");

int row = 0;

// Get new line

while (tokenLine = tokenizerLine.NextToken(tokenLine))

{
// For each line extracted, we add a row in the array

array.AddRow();

// The second tokenizer works with the extracted line

// to extract the data on a word by word basis.

// The "," separates words.

str tokenWord = null;

StringTokenizer tokenizerWord(tokenLine, ",");

int column = 0;

// Get new word

while (tokenWord = tokenizerWord.NextToken(tokenWord))

{
// Insert word in the array

array.SetElementStringValue(row, column, tokenWord);

++column;

}

++row;

}

}


编译VSL脚本并运行。要确认那个数组中的内容如下:





你可以看到,"Jane", "Pierre" 和 "John"这几个名字提取得不是很好,它们都以一个换行符开始(非打印换行符以一个小盒子的样子显示)。为了移除这个额外的字符,你需要给VSL脚本添加一个移除换行符的函数。下面的代码应该能完成这个任务:
void 		RemoveFirstReturnCharacter(String str2clear)

{
// If first character is equal to return...

if (str2clear[0] == '/n')
/ ... crop string from second character to the end

str2clear = str2clear.Crop(1, str2clear.Length()-1);

}


修改你的代码,要包括上面的函数。你的代码现在应该是像这个样子:
void main()

{
// We clear all data in the array

array.Clear();

// We create the first tokenizer in order to

// get data line by line

str tokenLine = null;

StringTokenizer tokenizerLine(data.CStr(), ".");

int row = 0;

// Get new line

while (tokenLine = tokenizerLine.NextToken(tokenLine))

{
// For each line extracted, we add a row in the array

array.AddRow();

// The second tokenizer works with the extracted line

// to extract the data on a word by word basis.

// The "," separates words.

str tokenWord = null;

StringTokenizer tokenizerWord(tokenLine, ",");

int column = 0;

// Get new word

while (tokenWord = tokenizerWord.NextToken(tokenWord))

{
// Remove first character if it's a '/n'

String strToClear = tokenWord;

RemoveFirstReturnCharacter(strToClear);

// Insert word in the array

array.SetElementStringValue(row, column, strToClear.CStr());

++column;

}

++row;

}

}

现在,在把单词插入数组之前,新的函数检查字符串并对之修改(如果有必要) - 移除换行符。

编译你的VSL脚本并运行。你的数组现在是不是看起来好多了?



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息