如何在PRAAT中创建字符串数组并实现元素检索?
在Praat中实现字符串数组创建与元素检索
Praat没有原生的字符串数组类型,但可以通过两种方式模拟实现你要的功能:
方法一:使用Strings对象(推荐)
Praat的Strings对象专门用来存储字符串集合,操作更直观:
1. 创建字符串集合(对应Python的options=["a","b","cd"])
# 直接创建包含3个元素的Strings对象 Create strings: "options", 3 Set string: "options", 1, "a" Set string: "options", 2, "b" Set string: "options", 3, "cd"
或者用简洁方式一次性生成:
Create strings as file list: "options", "a" + newline$ + "b" + newline$ + "cd"
2. 遍历检索元素(对应你的目标逻辑)
模拟Input集合并遍历检查:
# 创建Input集合 Create strings: "Input", 3 Set string: "Input", 1, "ab" Set string: "Input", 2, "cd" Set string: "Input", 3, "ef" # 遍历每个元素并检查是否在options中 for i from 1 to number_of_strings("Input") current$ = Get string: "Input", i # index_of_string返回元素位置,找不到则返回0 if index_of_string("options", current$) > 0 # 这里替换成你要执行的操作 writeInfoLine: "匹配到元素: ", current$ endif endfor
方法二:用分隔符拼接字符串模拟数组
如果不想创建对象,可使用换行符作为分隔符,将字符串拼接成长字符串模拟数组:
# 模拟options数组 options$ = "a" + newline$ + "b" + newline$ + "cd" # 模拟Input数组 Input$ = "ab" + newline$ + "cd" + newline$ + "ef" # 遍历Input的每个元素 current$ = extractLine$ (Input$, 1) i = 1 while current$ != "" # index_of_line检查元素是否存在,找不到返回0 if index_of_line(options$, current$) > 0 # 执行你的操作 writeInfoLine: "匹配到元素: ", current$ endif i = i + 1 current$ = extractLine$ (Input$, i) endwhile
内容的提问来源于stack exchange,提问作者curieux
相关产品推荐
相关产品推荐

