为什么推到一个数组时,我没有得到正确的价值? [英] Why I am not getting right value when pushing it to an array?
问题描述
我有一些数据的格式如下:
#AB测试
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - abTests:
productRanking:
版本:4
组:[
比:
默认值:1
美国:0.90
我:0.0
值:LessPopularityEPC
,
比:
默认值:0
美国:0.1
值:CtrEpcJob
,
比:
默认值:0
我:1.0
值:RandomPerVisitor
]
#路线
我想下面的输出作为一个字符串数组:
productRanking:LessPopularityEPC
productRanking:CtrEpcJob
productRanking:RandomPerVisitor
我用下面的code,将分开的数据和放大器的关键和价值观;将它们存储到一个数组
START_REGEXP = /#AB测试/
END_REGEXP = /#路线/
COMMENT_EXP = /#/#Function获取的关键是:价值与放大器;它们存储到阵列高清Automate_AB_tests.store_key_value(INPUT_FILE)
prev_line =
curr_line =
数组= []
标志=假 IO.foreach(INPUT_FILE)做|线|
prev_line = curr_line
curr_line =行
标志= true,如果行=〜START_REGEXP 如果标志
除非行=〜COMMENT_EXP
如果line.include? 版本:
关键= prev_line
把重点#productRanking:sabt:
结束 如果line.include? '值:'
值= line.delete('\\ n')。删除('')。拆分[1] #LessPopularityEPC CtrlEpcJob RandomPerVisitor
阵列<< #{}键:#{}值
结束
结束 标志= false如果行=〜END_REGEXP
结束
结束 把数组
结束
这是在获取密钥,但不存储这些密钥到字符串数组。如果任何人都可以指出什么地方错了我的code那么这将是真正伟大的。我得到如下的输出:
productRanking:
:LessPopularityEPC
:CtrEpcJob
:RadomPerVisitor
由于某种原因,你显然不能或不会安装Node.js的,这将使真的,这真的很短的工作,你就完蛋了做一个丑陋的黑客攻击。
我提出一个替代丑陋的黑客:CSON是不是所有的YAML不同。做一些简单的换人变成YAML,然后分析在Ruby中。
买者自负:像所有的丑陋的黑客,这是超级脆弱。当你试图去度假这可能会尽快打破。使用您自己的风险。
规定YAML数据=<< END
#AB测试
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - abTests:
productRanking:
版本:4
组:[
比:
默认值:1
美国:0.90
我:0.0
值:LessPopularityEPC
,
比:
默认值:0
美国:0.1
值:CtrEpcJob
,
比:
默认值:0
我:1.0
值:RandomPerVisitor
]
#路线
结束AB_TESTS_SECTION_EXPR = / ^(+)abTests:\\ N +(?= ^ \\ 1 [^])/ M#AB测试和#路由之间#提取文本
ab_tests =数据[AB_TESTS_SECTION_EXPR]#通过去除方括号打开CSON阵列到阵列YAML和
#逗号和插入`前各-`比
YAML = ab_tests.gsub(/ \\ s + \\ [$ /,'')
.gsub(/比:\\ S * $ / - 比)
.gsub(/ ^ \\ S * [\\] \\ S * $ /,'')把YAML
这将打印以下有效的YAML:
abTests:
productRanking:
版本:4
团体:
- 比:
默认值:1
美国:0.90
我:0.0
值:LessPopularityEPC - 比:
默认值:0
美国:0.1
值:CtrEpcJob - 比:
默认值:0
我:1.0
值:RandomPerVisitor
现在,我们只需要解析YAML到Ruby和提取我们需要的数据:
HSH = YAML.load(YAML)HSH [abTests]每做|。键,VAL |
VAL [组每个做|。集团|
把#{}键:#{组['值']}
结束
结束
#=> productRanking:LessPopularityEPC
#productRanking:CtrEpcJob
#productRanking:RandomPerVisitor
I have some data in the following format.
# AB Tests
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
abTests:
productRanking:
version: 4
groups: [
ratio:
default: 1
us: 0.90
me: 0.0
value: "LessPopularityEPC"
,
ratio:
default: 0
us: 0.1
value: "CtrEpcJob"
,
ratio:
default: 0
me: 1.0
value: "RandomPerVisitor"
]
# Routes
I want the following output as a string array:
productRanking:LessPopularityEPC
productRanking:CtrEpcJob
productRanking:RandomPerVisitor
I am using the following code which will separate the key and values from the data & store them into an array
START_REGEXP = /# AB Tests/
END_REGEXP = /# Routes/
COMMENT_EXP = /#/
#Function to fetch the key:value & store them into array
def Automate_AB_tests.store_key_value(input_file)
prev_line = ""
curr_line = ""
array = []
flag = false
IO.foreach(input_file) do |line|
prev_line = curr_line
curr_line = line
flag = true if line =~ START_REGEXP
if flag
unless line =~ COMMENT_EXP
if line.include? 'version: '
key = prev_line
puts key #productRanking: sabt:
end
if line.include? 'value: '
value = line.delete('\n').delete('"').split[1] #LessPopularityEPC CtrlEpcJob RandomPerVisitor
array << "#{key}:#{value}"
end
end
flag = false if line =~ END_REGEXP
end
end
puts array
end
It is fetching the keys but not storing those keys into stringArray. If anyone can point out what's wrong with my code then it would be really great. I am getting output as below:
productRanking:
:LessPopularityEPC
:CtrEpcJob
:RadomPerVisitor
Since for some reason you apparently can't or won't install Node.js, which would make really, really short work of this, you're stuck doing an ugly hack.
I propose an alternate ugly hack: CSON isn't all that different from YAML. Do some simple substitutions to turn it into YAML and then parse that in Ruby.
Caveat emptor: Like all ugly hacks, this is super fragile. It will probably break as soon as you try to take a vacation. Use at your own risk.
require "yaml"
data = <<END
# AB Tests
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
abTests:
productRanking:
version: 4
groups: [
ratio:
default: 1
us: 0.90
me: 0.0
value: "LessPopularityEPC"
,
ratio:
default: 0
us: 0.1
value: "CtrEpcJob"
,
ratio:
default: 0
me: 1.0
value: "RandomPerVisitor"
]
# Routes
END
AB_TESTS_SECTION_EXPR = /^( +)abTests:\n.+(?=^\1[^ ])/m
# Extract text between "# AB Tests" and "# Routes"
ab_tests = data[AB_TESTS_SECTION_EXPR]
# Turn the CSON array into a YAML array by removing the square brackets and
# commas and inserting a `-` before each "ratio:"
yaml = ab_tests.gsub(/\s+\[$/, '')
.gsub(/ ratio:\s*$/, '- ratio:')
.gsub(/^\s*[,\]]\s*$/, '')
puts yaml
This will print the following valid YAML:
abTests:
productRanking:
version: 4
groups:
- ratio:
default: 1
us: 0.90
me: 0.0
value: "LessPopularityEPC"
- ratio:
default: 0
us: 0.1
value: "CtrEpcJob"
- ratio:
default: 0
me: 1.0
value: "RandomPerVisitor"
Now we just need to parse the YAML into Ruby and extract the data we need:
hsh = YAML.load(yaml)
hsh["abTests"].each do |key, val|
val["groups"].each do |group|
puts "#{key}:#{group['value']}"
end
end
# => productRanking:LessPopularityEPC
# productRanking:CtrEpcJob
# productRanking:RandomPerVisitor
这篇关于为什么推到一个数组时,我没有得到正确的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!