获取数组中三个连续元素的值

时间:2022-01-06 10:31:16

I have a large array, h, that contains several instances of a parameter called startingParam, which is always followed by two other parameters that are related but not always the same. I need to look for every instance of startingParam in the array, and push it and the next two parameters into a separate array, holdingArray.

我有一个很大的数组h,它包含一个名为startingParam的参数的几个实例,它后面总是跟着另外两个相关但并不总是相同的参数。我需要在数组中查找startingParam的每个实例,并将它和接下来的两个参数推入一个单独的数组,holdingArray。

The following code is not working, due to the fact that I am very new to Ruby. Does anybody know what I am doing wrong? Is there a better way to approach the problem?

下面的代码不能工作,因为我对Ruby非常陌生。有人知道我做错了什么吗?有更好的方法来解决这个问题吗?

h.each do |param|
    if param == 'startingParam'
        holdingArray << h.[param],
        holdingArray << h.[param + 1],
        holdingArray << h.[param + 2]
    end
end

Thanks so much.

非常感谢。

4 个解决方案

#1


2  

So there are a few problems. For starters, you can't subscript arrays by doing h.[anything], and you are also subscripting based on the value (and not the index). You are also checking to see if the parameter matches the literal string "starting_param" and not its value. So what I expect you want is the following:

所以有几个问题。首先,你不能通过做h来下标数组。[任何东西],并且您也基于值(而不是索引)进行下标。您还将检查参数是否与文字字符串“starting_param”匹配,而不是其值。所以我希望你想要的是:

h.each_with_index do |param, index|
  if param == startingParam
    holdingArray << h[index]
    holdingArray << h[index+1]
    holdingArray << h[index+2]
  end
end

You'll also note that if the item is in the last two slots of the array, this will wrap around and grab items from the beginning of the array (due to how Ruby handles array subscripts being out of bounds).

您还会注意到,如果该项目位于数组的最后两个插槽中,那么它将会从数组的开始处打包并抓取项目(由于Ruby是如何处理数组下标的)。

#2


5  

You can grab the chunks using #slice_before:

您可以使用#slice_before来获取这些块:

arr = ['startingParam', 1, 2, 'startingParam', 3, 4]

arr.slice_before('startingParam')
# => [['startingParam', 1, 2], ['startingParam', 3, 4]]

If you created the original data structure, you may want to re-consider your design, however.

但是,如果您创建了原始的数据结构,您可能需要重新考虑您的设计。

#3


3  

Functional approach:

功能的方法:

>> ary = ['hello', 'startingParam', 1, 2, 'xyz', 'startingParam', 3, 4, 'bye']    
>> ary.each_cons(3).select { |v, *vs| v == "startingParam" }.flatten(1)
=> ["startingParam", 1, 2, "startingParam", 3, 4]

#4


1  

You could also use the range slicing operation (I've changed the varnames slightly since camelcasing is bad style in ruby)

您还可以使用range切片操作(我已经稍微更改了varname,因为camel外壳在ruby中是不好的样式)

h.each_with_index do |param, index|
  if param == starting_param
     holding_array.push(h[index..index+2])
  end
end

#1


2  

So there are a few problems. For starters, you can't subscript arrays by doing h.[anything], and you are also subscripting based on the value (and not the index). You are also checking to see if the parameter matches the literal string "starting_param" and not its value. So what I expect you want is the following:

所以有几个问题。首先,你不能通过做h来下标数组。[任何东西],并且您也基于值(而不是索引)进行下标。您还将检查参数是否与文字字符串“starting_param”匹配,而不是其值。所以我希望你想要的是:

h.each_with_index do |param, index|
  if param == startingParam
    holdingArray << h[index]
    holdingArray << h[index+1]
    holdingArray << h[index+2]
  end
end

You'll also note that if the item is in the last two slots of the array, this will wrap around and grab items from the beginning of the array (due to how Ruby handles array subscripts being out of bounds).

您还会注意到,如果该项目位于数组的最后两个插槽中,那么它将会从数组的开始处打包并抓取项目(由于Ruby是如何处理数组下标的)。

#2


5  

You can grab the chunks using #slice_before:

您可以使用#slice_before来获取这些块:

arr = ['startingParam', 1, 2, 'startingParam', 3, 4]

arr.slice_before('startingParam')
# => [['startingParam', 1, 2], ['startingParam', 3, 4]]

If you created the original data structure, you may want to re-consider your design, however.

但是,如果您创建了原始的数据结构,您可能需要重新考虑您的设计。

#3


3  

Functional approach:

功能的方法:

>> ary = ['hello', 'startingParam', 1, 2, 'xyz', 'startingParam', 3, 4, 'bye']    
>> ary.each_cons(3).select { |v, *vs| v == "startingParam" }.flatten(1)
=> ["startingParam", 1, 2, "startingParam", 3, 4]

#4


1  

You could also use the range slicing operation (I've changed the varnames slightly since camelcasing is bad style in ruby)

您还可以使用range切片操作(我已经稍微更改了varname,因为camel外壳在ruby中是不好的样式)

h.each_with_index do |param, index|
  if param == starting_param
     holding_array.push(h[index..index+2])
  end
end