Ruby:有序数组并添加到下一个数字的数组(如果数字存在)
我想知道是否有人知道一种按数字组织数组的简单方法,但如果数字已经存在,则将其推送到下一个不存在的数字,我正在考虑创建一个多维有序数组,其中如果数字冲突(例如 2 页有 1) 那么第一个将是 [1][1],第二个将是 [1][2] 但有没有更好的方法来处理这个问题?
编辑;一个例子:
page1 -> sets order to 1
page2 -> sets order to 1
page3 -> sets order to 2
通常我会通过 YAML 读取页面配置并获取顺序,然后使用该数字并设置 _site.sidebar[_config["order"]]
但在这种情况下它会发生冲突它不会添加它。因此,我正在寻找一种允许用户错误但保留顺序的方法,将找到的第一个 1 作为一个,但如果存在,则将数组向下移动并将第二个 1 作为两个。
I was wondering if anyone new of an easy way to organize an array by numbers but if the number already exists push it to the next number that doesn't exist I was thinking of just creating a multi-dimensional ordered array where if numbers clash (such as 2 pages having 1) then the first would be [1][1] and the second would be [1][2] but is there a better way to handle this?
Edit; an example:
page1 -> sets order to 1
page2 -> sets order to 1
page3 -> sets order to 2
Normally I would go through and YAML read the pages configurations and get the order and then use that number and set _site.sidebar[_config["order"]]
but in this case it would clash and it wouldn't add it. So I'm looking for a way to allow for user mistakes but preserve order keeping the first found as one but if one exists shift the array down and put the second 1 as two.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这听起来像是您正在实现一个哈希表,并使用“数字”作为哈希。有各种各样的算法,只需寻找哈希表算法即可。
This sounds like you're implementing a hashtable, and using 'number' as hash. There are all kinds of algorithms for that, just look for hashtable algorithms.
这是我如何实现我所询问的内容的最后一个片段,以防万一其他人偶然发现这个线程来寻找相同的东西。我基本上只是想保留顺序,在代码的实际应用中,我使用了普通的多维数组,因为“顺序”是从 YAML 前面提取的,所以它是它自己的变量。
Here is the final snippet on how I implemented what I was asking about, just in case somebody else stumbles upon this thread looking for the same sort of thing. I basically just wanted to preserve the order, in my actual application of the code I used a normal multi-dimensional array since "order" was pulled from YAML front so it is it's own variable.