创建一个从 A 开始到 Z 结束的字母范围的数组,即(B 到 AC) [英] Create an array of a range of letters starting after A and finishing after Z ie (B to AC)

查看:44
本文介绍了创建一个从 A 开始到 Z 结束的字母范围的数组,即(B 到 AC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Roo Gem 并希望从基于标准 A1 语法的电子表格中提取数据.

I am working with the Roo Gem and wanted to pull data from a spreadsheet based on standard A1 syntax.

我在电子表格中有 Z 以外的列,所以 Excel 会处理整个 AA、AB、AC 列位置.

I have columns in the spreadsheet beyond Z so Excel does the whole AA, AB, AC column positions.

我想为列 W 到 AH 创建一个数组.

I want to create an array for columns W to AH.

Ruby 似乎不喜欢上限超出 Z 但尚未从 A 开始的情况??

Ruby doesn't seem to like when the upper range extends past Z but hasn't started from A??

任何想法如何 ("B".."AC").to_a 而不是得到 []

Any ideas how to ("B".."AC").to_a and not get []

这里是 irb 的基本问题.

Here is the basic problem in irb.

("A".."Z").to_a
#=> ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
("B".."Z").to_a
#=> ["B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
("A".."AC").to_a
#=> ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC"]
("B".."AC").to_a
#=> []

推荐答案

这个怎么样?

("A".."AC").to_a.drop(1)

你可以删除你喜欢的任意数量的元素,它只涉及1个范围和1个数组的创建..

you can drop whatever number of elements you like and it just involves 1 range and 1 array creation..

整数可能会被某些返回字母在字母表中的位置的东西所替代.

The integer could potentially be substituted for someting that returns the position of the letter in the alphabet.

class Array
  def from(column)
    drop(find_index(column).to_i)
  end
end

("A".."AC").to_a.from('F')
#=> ["F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC"]

直接使用 Range 类,感谢 @sagarpandya82

Using Range class directly, thanks to @sagarpandya82

class Range
  def from(column)
    to_a.drop(find_index(column).to_i)
  end
end

("A".."AC").from('F')
#=> ["F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC"]

这篇关于创建一个从 A 开始到 Z 结束的字母范围的数组,即(B 到 AC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆