如何使用每个元素从第二个元素开始在数组中循环? - Ruby

时间:2022-12-03 19:25:08

Say I have an array like this:

假设我有一个这样的数组:

["auburn", "http://auburn.craigslist.org/web/", "http://auburn.craigslist.org/cpg/", "http://auburn.craigslist.org/eng/", "http://auburn.craigslist.org/sof/", "http://auburn.craigslist.org/sad/"]

What I would like to do is work on just the URLs in this array - which will always start at element[1] and go up.

我想要做的只是处理这个数组中的URL - 它总是从元素[1]开始然后上升。

How do I do that?

我怎么做?

5 个解决方案

#1


11  

This only shows the elements starting from 1 (the second element), -1 stands for the last element

这仅显示从1开始的元素(第二个元素),-1代表最后一个元素

a = ["auburn", "http://auburn.craigslist.org/web/", "http://auburn.craigslist.org/cpg/", "http://auburn.craigslist.org/eng/", "http://auburn.craigslist.org/sof/", "http://auburn.craigslist.org/sad/"] 

p a[1..-1]
=> ["http://auburn.craigslist.org/web/", "http://auburn.craigslist.org/cpg/", "http://auburn.craigslist.org/eng/", "http://auburn.craigslist.org/sof/", "http://auburn.craigslist.org/sad/"]

#2


9  

a = ["auburn", "http://auburn.craigslist.org/web/", "http://auburn.craigslist.org/cpg/", "http://auburn.craigslist.org/eng/", "http://auburn.craigslist.org/sof/", "http://auburn.craigslist.org/sad/"]

a.drop(1).each { |m| puts m }

#=> http://auburn.craigslist.org/web/
#   http://auburn.craigslist.org/cpg/
#   http://auburn.craigslist.org/eng/
#   http://auburn.craigslist.org/sof/
#   http://auburn.craigslist.org/sad/

#3


3  

a[1..-1] will return the array with the first item removed.

a [1 ..- 1]将返回数组,并删除第一个项目。

#4


2  

What about just simply choosing the elements you need:

只需选择您需要的元素即可:

a.grep(/^http:/).each { ... }

Or if you don't want new array copies to be created unnecessarily:

或者,如果您不希望不必要地创建新的阵列副本:

a.each { |e| 
  next if !e.match(/^http:/)
  ...
}

#5


1  

try slice :

尝试切片:

arr = ["auburn", "http://auburn.craigslist.org/web/", "http://auburn.craigslist.org/cpg/", "http://auburn.craigslist.org/eng/", "http://auburn.craigslist.org/sof/", "http://auburn.craigslist.org/sad/"]
arr.slice(1..-1)

edit :

use it like :

使用它像:

arr.slice(1..-1).each do |p|
  puts p
end

#1


11  

This only shows the elements starting from 1 (the second element), -1 stands for the last element

这仅显示从1开始的元素(第二个元素),-1代表最后一个元素

a = ["auburn", "http://auburn.craigslist.org/web/", "http://auburn.craigslist.org/cpg/", "http://auburn.craigslist.org/eng/", "http://auburn.craigslist.org/sof/", "http://auburn.craigslist.org/sad/"] 

p a[1..-1]
=> ["http://auburn.craigslist.org/web/", "http://auburn.craigslist.org/cpg/", "http://auburn.craigslist.org/eng/", "http://auburn.craigslist.org/sof/", "http://auburn.craigslist.org/sad/"]

#2


9  

a = ["auburn", "http://auburn.craigslist.org/web/", "http://auburn.craigslist.org/cpg/", "http://auburn.craigslist.org/eng/", "http://auburn.craigslist.org/sof/", "http://auburn.craigslist.org/sad/"]

a.drop(1).each { |m| puts m }

#=> http://auburn.craigslist.org/web/
#   http://auburn.craigslist.org/cpg/
#   http://auburn.craigslist.org/eng/
#   http://auburn.craigslist.org/sof/
#   http://auburn.craigslist.org/sad/

#3


3  

a[1..-1] will return the array with the first item removed.

a [1 ..- 1]将返回数组,并删除第一个项目。

#4


2  

What about just simply choosing the elements you need:

只需选择您需要的元素即可:

a.grep(/^http:/).each { ... }

Or if you don't want new array copies to be created unnecessarily:

或者,如果您不希望不必要地创建新的阵列副本:

a.each { |e| 
  next if !e.match(/^http:/)
  ...
}

#5


1  

try slice :

尝试切片:

arr = ["auburn", "http://auburn.craigslist.org/web/", "http://auburn.craigslist.org/cpg/", "http://auburn.craigslist.org/eng/", "http://auburn.craigslist.org/sof/", "http://auburn.craigslist.org/sad/"]
arr.slice(1..-1)

edit :

use it like :

使用它像:

arr.slice(1..-1).each do |p|
  puts p
end