如何输入整数值数组,以preceeding行+列的值?红宝石 [英] How to input integer value to an array, based preceeding row + column values? RUBY
问题描述
有关这个项目之后,我应该把输入的格式如下:R1C5 + 2,其内容为在餐桌上,一排5列1,加2或者格式为:R1C2C3-5,这曰:在餐桌上,排1月2日至3日柱,减去5这是假设表中所有数字是最初所有0
For this following project, I am supposed to take input in the following format : R1C5+2 , which reads it as "in the table, Row 1 Column 5 ,add 2. Or in this format : R1C2C3-5 , which reads : "in the table, Row 1 Column 2-3, subtract 5. This is assuming that all numbers in the table are initially all 0.
在哪里我离开的:
我无法找到一个方法来检测一个+或 - 要么在表中加/减值。此外,在提供一系列允许多个补充提供两个C的或R级的时候。例如:R1R5C2C3 + 2(行范围1 - 5,列范围2 - 3,加2)。
I am having trouble finding a way to detect for a "+" or "-" to either add/subtract values in the table. Also, in providing a range to allow multiple additions when provided two C's or R's. For example: R1R5C2C3+2 (Row Range 1 - 5, Column Range 2 - 3, add 2).
下面是下列code:
puts 'Please input: '
x = gets.chomp
col = []
row = []
x.chars.each_slice(2) { |u| u[0] == "R" ? row << u[1] : col << u[1] }
p col
p row
puts "Largest # in Row array: #{row.max}"
puts "Largest # in Columns array: #{col.max}" #must be in "" to return value
big_row = row.max.to_i
big_col = col.max.to_i
table = Array.new (big_row) { Array.new(big_col) }
我谢谢大家的帮助!
I thank you all for the help!
推荐答案
您正在寻找的方法是 =〜
运营商。如果你使用它的一个字符串,并给它一个正规表达式模式,将返回字符串中的模式的位置。因此:
The method you are looking for is the =~
operator. If you use it on a string and give it a regexp pattern it will return the location of that pattern in the string. Thus:
x = 'R1C2C3-5'
x =~ /R/
收益: 0
自认为是'R'字符串中的位置(计就像一个数组0,1,2 ...)
returns: 0
since that is the position of 'R' in the string (counted just like an array 0,1,2...).
如果您不熟悉正则表达式和 =〜
运营商,我建议你检查出的Ruby文档就可以了,这是非常有价值的。基本上斜杠的模式得到匹配。您正在寻找匹配 +
或 -
,但它们在正则表达式特殊的意义,所以你有逃避他们反斜杠。
If you are unfamiliar with regexp and the =~
operator, I suggest you check out the Ruby doc on it, it is very valuable. Basically the pattern between the forward slashes get matched. You are looking to match +
or -
, but they have special meaning in regexp, so you have to escape them with a backslash.
x =~ /\+/
x =~ /\-/
但你可以结合这些成一个模式匹配带或符号(管道) |
x =~ /\+|\-/
所以,现在你有一个方法来获得操作:
So now you have a method to get the operator:
def operator(my_string)
r = my_string.slice(my_string =~ /\+|\-/)
end
我也将使用运营商的字符串分割成列/行部分和数字部分:
I would also use the operator to split your string into the column/row part and the numeric part:
op = operator(x) # which returns op = '-'
arr = x.split(my_string(x)) # which returns an array of two strings ['R1C2C3', '5']
我留下进一步字符串操作给你。我将通过对String类此页阅读: Ruby的String类这对数组:红宝石Array类如红宝石包含了如此许多方法,使这样的事情更容易。有一件事情我已经学会使用Ruby做的是想我要做到这一点,我不知道是否已经有一个内置的方法来做到这一点?我去检查的文档。更是让使用Rails!
I leave further string manipulation up to you. I would read through this page on the String class: Ruby String Class and this on arrays: Ruby Array Class as Ruby contains so many methods to make things like this easier. One thing I've learned to do with Ruby is think "I want to do this, I wonder if there is already a built in method to do this?" and I go check the docs. Even more so with Rails!
这篇关于如何输入整数值数组,以preceeding行+列的值?红宝石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!