gpt4 book ai didi

Ruby:从二维数组中的每个子数组中获取第 n 个元素

转载 作者:太空宇宙 更新时间:2023-11-03 17:25:25 26 4
gpt4 key购买 nike

作为 Watir 测试套件的一部分,我们正在验证数据表。其中一项要求是特定列必须为空(除非选择了特定的单选按钮)所以在我的脚本中,我有以下代码

incidents = browser.table(:xpath, Xpath_CC).strings 

其中 browser 是一个 watir-webdriver 浏览器对象在事件中给我这个二维数组

[["DateTime", "Problem", "Value", "Compared", "Number", "Causes", "Where", "Count", "Type"],
["May 07, 02:15 - 02:30", "Web", "38.46 %", "", "1", "UNKNOWN", "", "1", "J105"],
["May 07, 02:15 - 02:30", "Video", "40 %", "", "1", "UNKNOWN", "", "1", "J105"],
["May 07, 02:00 - 02:15", "Email", "30 %", "", "1", "UNKNOWN", "", "1", "J105"],
["May 07, 01:00 - 01:15", "Email", "70 %", "", "1", "UNKNOWN", "", "1", "J105"],
["May 07, 01:00 - 01:15", "Email", "70 %", "", "1", "UNKNOWN", "", "1", "J105"],
["May 07, 01:00 - 01:15", "Email", "0 %", "", "1", "UNKNOWN", "", "1", "J105"],
]]

我需要做的是检查每个子数组中的每个“Compared”列并确保它是空的。我可以做类似的事情

 result = true
incidents.each do |row|
if (row[3] != '')
result = false
end
end

这是我的 Java 大脑告诉我如何去做的。我想这一定是一种更简单的单行 ruby​​ 方式来做到这一点

最佳答案

一行:

result = incidents.all? { |row| row[3].empty? }

跳过标题信息:

result = incidents[1..-1].all? { |row| row[3].empty? }

关于Ruby:从二维数组中的每个子数组中获取第 n 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13397127/

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