Ruby:有序数组并添加到下一个数字的数组(如果数字存在)

发布于 2024-11-29 19:17:52 字数 442 浏览 0 评论 0原文

我想知道是否有人知道一种按数字组织数组的简单方法,但如果数字已经存在,则将其推送到下一个不存在的数字,我正在考虑创建一个多维有序数组,其中如果数字冲突(例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懒的傷心 2024-12-06 19:17:52

这听起来像是您正在实现一个哈希表,并使用“数字”作为哈希。有各种各样的算法,只需寻找哈希表算法即可。

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.

人│生佛魔见 2024-12-06 19:17:52

这是我如何实现我所询问的内容的最后一个片段,以防万一其他人偶然发现这个线程来寻找相同的东西。我基本上只是想保留顺序,在代码的实际应用中,我使用了普通的多维数组,因为“顺序”是从 YAML 前面提取的,所以它是它自己的变量。

data = []
demo = {
  "page_1" => {
    "order" => 1,
    "data" => "Hello World 1"
  },
  "page_2" => {
    "order" => 2,
    "data" => "Hello World 2"
  },
  "page_3" => {
    "order" => 1,
    "data" => "Hello World 3"
  },
  "page_4" => {
    "order" => "a",
    "data" => "Hello World 4"
  }
}

demo.each |key, page|
  local_data = page["data"]
  order      = page["order"].to_i.floor

  data[order] ||= []
  data[order] << local_data

}
puts data.flatten.join(" ").strip

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.

data = []
demo = {
  "page_1" => {
    "order" => 1,
    "data" => "Hello World 1"
  },
  "page_2" => {
    "order" => 2,
    "data" => "Hello World 2"
  },
  "page_3" => {
    "order" => 1,
    "data" => "Hello World 3"
  },
  "page_4" => {
    "order" => "a",
    "data" => "Hello World 4"
  }
}

demo.each |key, page|
  local_data = page["data"]
  order      = page["order"].to_i.floor

  data[order] ||= []
  data[order] << local_data

}
puts data.flatten.join(" ").strip
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文