Arrays 如何在嵌套数组中查找特定值?
我试图找出如何将一个值放入三个数组中的一个,然后将这些数组洗牌,让程序输出值的索引位置 以下是我到目前为止的情况:Arrays 如何在嵌套数组中查找特定值?,arrays,ruby,multidimensional-array,indexing,Arrays,Ruby,Multidimensional Array,Indexing,我试图找出如何将一个值放入三个数组中的一个,然后将这些数组洗牌,让程序输出值的索引位置 以下是我到目前为止的情况: # The purpose of this program is to randomly place the name Zac # in one of three arrays and return the array number and position of # Zac A1 = ["John","Steve","Frank
# The purpose of this program is to randomly place the name Zac
# in one of three arrays and return the array number and position of
# Zac
A1 = ["John","Steve","Frank","Charles"]
A2 = ["Sam","Clint","Stuart","James"]
A3 = ["Vic","Jim","Bill","David"]
n = [A1,A2,A3]
name = "Zac"
def placename(title, namelist)
mix = rand(2)
namelist[mix] << title
namelist.shuffle
return namelist
end
allnames = [] << placename(name, n)
def findname(allnames, key)
allnames.each do |i|
until allnames[i].include?(key) == true
i+=1
end
location = allnames[i].find_index(key)
puts "The location and value of #{key} is #{location}"
end
end
findname(allnames, name)
#此程序的目的是随机放置名称Zac
#在三个数组中的一个数组中,返回
#扎克
A1=[“约翰”、“史蒂夫”、“弗兰克”、“查尔斯”]
A2=[“萨姆”、“克林特”、“斯图尔特”、“詹姆斯”]
A3=[“维克”、“吉姆”、“比尔”、“大卫”]
n=[A1,A2,A3]
name=“Zac”
def地名(标题、名称列表)
混合=兰特(2)
名字列表[mix]我修改了最后几行。我希望这就是你想要的
allnames=placename(名称,n)
def findname所有名称,键
r=allnames.map.with_索引{x,i | x.include?(键)?i:p}-[p]
放置“值#{key}的位置是数组号#{r[0]}和项目号#{allnames[r[0]]].index(key)}”
结束
findname(所有名称、名称)
编辑:随机化
要获得随机化数组编号和项目编号,必须执行以下操作
def placename(标题、名称列表)
mix=rand(3)#由于数组(嵌套在其中)的数量是3,我们可以使用3而不是2
namelist[mix]您的方法假设在块开始时
allnames.each do |i|
。。。i
将包含allnames元素的索引。这不是真的<代码>i
将包含元素的值(内容)
你可以尝试的另一种选择是
allnames.each_with_index do |_value, i|
或者,你可以
allnames.each do |value|
然后将对所有名称[i]
的所有引用替换为值
另一个问题是
allnames = [] << placename(name, n)
假设您希望就地修改阵列,我会这样做:
# insert name into random subarray
def insert_name name
subarray_idx = rand @name_arrays.size
subarray = @name_arrays[subarray_idx]
insertion_idx = rand subarray.size
@name_arrays[subarray_idx].insert insertion_idx, name
sprintf '"%s" inserted at @name_arrays[%d][%d]',
name, subarray_idx, insertion_idx
end
# define starting array, then print & return the
# message for further parsing if needed
@name_arrays = [
%w[John Steve Frank Charles],
%w[Sam Clint Stuart James],
%w[Vic Jim Bill David],
]
p(insert_name 'Zac')
这有几个好处:
您可以检查@name_数组,以验证其外观是否符合预期
如果需要,可以使用解析消息
您可以修改#insert_name以返回索引,而不必直接搜索名称
如果您不将插入索引捕获为返回值,或者不想从消息字符串中解析它,您可以通过利用和来搜索它。例如:
#仅用于演示,设置此选项可以获得相同的效果
#结果由于插入指数是随机的
@名称\u数组=
[[“约翰”、“史蒂夫”、“弗兰克”、“查尔斯”],
[“山姆”、“克林特”、“斯图尔特”、“詹姆斯”],
[“维克”、“吉姆”、“扎克”、“比尔”、“大卫”]]
#嵌套匹配的返回索引
def find_name_idx name
@命名数组。每个数组都有索引
.map{[_2,_1.index(name)]}
.拒绝{u 1.any?nil}
爸爸
结束
#使用数组#dig在嵌套索引处检索项
@name_arrays.dig*find_name_idx('Zac'))
Tip:不必费心声明中间数组,只需做这一步,比如NAMES=[[“John”、…]、[…]]
placename
真的是n.样本塔德曼-我很感谢这个提示!在这个例子中,我实际上只是想学习如何从多维数组中访问指定值的索引号。就我个人所知。这正是我打算在这篇文章中重点关注的部分。谢谢你的帮助!!placename点也很有意义,谢谢。错误消息告诉您a)您试图调用的方法的名称,b)调用发生在哪个方法中,c)错误发生在哪个文件中,d)错误发生在哪个行中。它告诉您查找错误所需的所有信息。堆栈跟踪告诉您如何到达错误发生的位置。因为你是唯一拥有这些信息的人,所以你是唯一能够真正解决问题的人。嘿,约翰-我注意到这个程序每次都输出相同的数组号和项目号。程序应该在每次执行时对值“Zac”进行洗牌,并将其放入不同的数组和项目编号中。你能解释一下出了什么问题吗?请参阅编辑@zspark94Steve-感谢allnames=placename(name,n)
我想知道为什么它被嵌套了两次!!你能告诉我为什么在allnames的块中有两个值。每个带有| index do | value,i |
?我只是想了解,是否要将元素的值存储在第一个变量(_值)中,并将索引号存储在第二个变量(I)中?或者它是如何工作的?是的,确切地说,each
t返回值,each\u with\u index
返回值和索引。由于您实际上没有引用该值,因此惯例是在变量名称前面加下划线\u值
,或者,您可以返回到每个
。。。我会更新我的答案以反映这一点。这个答案有点超出我的经验水平。我将不得不在稍后的时间提及这一点,这样它才更有意义。
# insert name into random subarray
def insert_name name
subarray_idx = rand @name_arrays.size
subarray = @name_arrays[subarray_idx]
insertion_idx = rand subarray.size
@name_arrays[subarray_idx].insert insertion_idx, name
sprintf '"%s" inserted at @name_arrays[%d][%d]',
name, subarray_idx, insertion_idx
end
# define starting array, then print & return the
# message for further parsing if needed
@name_arrays = [
%w[John Steve Frank Charles],
%w[Sam Clint Stuart James],
%w[Vic Jim Bill David],
]
p(insert_name 'Zac')