gpt4 book ai didi

ruby - 迭代数组中的每个单词

转载 作者:数据小太阳 更新时间:2023-10-29 08:29:04 25 4
gpt4 key购买 nike

我有一个非常大的 .txt 文件,我想编写一个 ruby​​ 脚本来过滤一些数据。基本上我想遍历每一行,然后将行中的单个单词存储在一个数组中,然后对这些单词进行操作。但是我无法在数组中单独获取每个单词

tracker_file.each_line do|line|
arr = "#{line}"

我可以像这样得到整行,但是单个单词呢?

谢谢

最佳答案

对字符串使用 split 方法。

irb(main):001:0> line = "one two three"
=> "one two three"
irb(main):002:0> line.split
=> ["one", "two", "three"]

所以你的例子是:

tracker_file.each_line do |line|
arr = line.split
# ... do stuff with arr
end

关于ruby - 迭代数组中的每个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5916634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com