首页 \ 问答 \ 通过哈希对象循环获取次数(Get number of times in loop over Hash object)

通过哈希对象循环获取次数(Get number of times in loop over Hash object)

我有一个Hash类型的对象,我想通过hash.each do |key, value|来循环 。 我想知道从1开始循环的次数。

是否有类似于each提供此方法的方法(同时仍提供散列键/值数据),还是需要创建另一个计数器变量以在循环内增加?


I have an object of type Hash that I want to loop over via hash.each do |key, value|. I would like to get the number of times I've been through the loop starting at 1.

Is there a method similar to each that provides this (while still providing the hash key/value data), or do I need to create another counter variable to increment within the loop?


原文:https://stackoverflow.com/questions/2601293
更新时间:2020-10-25 10:10

最满意答案

使用each_with_index而不是each 。 注意:索引确实从0开始:

hash.each_with_index do |(key, value), index|

Use each_with_index instead of each. Note: the index does start at 0:

hash.each_with_index do |(key, value), index|
2010-04-08

相关问答

更多

如何在JSTL中循环指定次数?(How to loop over something a specified number of times in JSTL?)

<c:forEach>标签绝对适用于此。 它具有begin和end属性,您可以在其中指定,以及开始和结束。 它有一个varStatus属性,它将一个LoopTagStatus对象放在循环标签范围内,后者又有几个方法,如getIndex()和on。 这是一个开球示例: <c:forEach begin="0" end="10" varStatus="loop"> Index: ${loop.index}<br/> </c:forEach> The <c:forEach> tag is de ...

如何重复一次while循环一定次数(How to repeat a while loop a certain number of times)

如@ R2RT所述,您需要在每个r循环后重置w 。 试着写这个: import random a = [] w = 0 r = 0 while r < 3: while w<4: x = random.uniform(1,10) print(x) print(w) a.append(w+x) print(a) w = w+1 r += 1 ...

在哈希表中循环日期?(Loop through dates in hash table?)

由于哈希表键是[datetime]对象 ,因此根本不需要使用日期字符串和字符串解析: $today = (Get-Date).Date # Note the .Date part, which omits the time portion $startdate = $today.AddDays(-30) # Note the change from -lt to -le to include today for($i = $startdate; $i -le $today; $i = $i.Ad ...

保持无限循环执行次数的计数(keeping count of the number of times an infinite loop executes)

只有一个柜台 $count = 0; while (true) { //break after it has executed 1000 times if ($count == 1000) break; $count++; } just have a counter $count = 0; while (true) { //break after it has executed 1000 times if ($count == 1000) break; $count+ ...

Rails循环内部哈希(Rails loop inside hash)

编辑协议编辑的问题 您可以在哈希内部执行函数而不会出现问题,我认为问题是您如何使用哈希和函数。 看(我会使用reduce而不是foreach因为我更喜欢它,但它是一样的): all_urls = ["example.com/homepage", "example.com/another", "example.com"] hash = { "type":"condition", "Params":{ "rules":{ "condition": ...

如何计算对象属性已更改的次数(How to count how many times an object property has changed)

您可以使用window.onhashchange事件,在每次更改时增加一个计数器......这对您有用吗? var changes = 0; window.onhashchange = x=> changes++; 对于其他对象/属性,您可以使用相同的原则,使用GETTERS或PROXIES。 另一种选择可能是使用一些允许你观察变量的库。 一种选择可能是WatchJS You could use the window.onhashchange event, to increase a count ...

哈希映射和哈希码更改,如何告诉set该对象已更改?(Hash maps and hashcode that changes, how to tell set that object has changed?)

在对象的生命周期中,对象的哈希假定为常量,因此对您的问题的严格回答是:否。 当您修改对象使其哈希码更改时,您最好将其从地图中删除并再次添加回来。 Object's hash assumed to be constant during the object's lifetime, so strict answer to your question is: no. When you modify your object such that it's hash code is changed, you' ...

表达式在for循环中计算的R次数(R number of times an expression evaluates in a for loop)

list.files只会在你提出的for循环中使用它时评估一次。 测试这个的最简单方法是在另一个函数调用中包含对list.files调用,如下所示: f <- function() { print("Calling f"); list.files() } 并在for循环中使用它: for (infile in f()) { print(infile) } list.files will only be evaluated once when you use it in a for-lo ...

计算循环执行次数(Counting how many times a loop is performed)

根据你的代码和变量名称。 你可以保持这样的结果...... var rolls = new Array(); var maxMoney = 0;//Max Money var rollCountMoney = new Array(); var count = 0; function rollDice() { do { count++; var userInput = parseInt(document.getElementById("bet").value); ...

通过哈希对象循环获取次数(Get number of times in loop over Hash object)

使用each_with_index而不是each 。 注意:索引确实从0开始: hash.each_with_index do |(key, value), index| Use each_with_index instead of each. Note: the index does start at 0: hash.each_with_index do |(key, value), index|

相关文章

更多

最新问答

更多
  • 如何保护Solr只允许SELECT请求给用户并禁止其他任何东西?(How to secure Solr to allow SELECT request to only users and disallow anything else?)
  • XPath表达式无效/错误TFHpple SWIFT 1.2(XPath Expression not working/incorrect TFHpple SWIFT 1.2)
  • css3 3D变换不能平滑地制作动画(css3 3D transform doesn't animate smoothly)
  • 运行时错误'91'和Outlook.Application = <对象变量或没有设置块变量>?(Run-time error '91' & Outlook.Application = Object variable or With block variable not set?)
  • 慢的webservice问题(Slow webservice problem)
  • textview的不正确对齐方式(Improper alignment of a textview)
  • 在第一步“Hello World”中出现Java错误(Cannot run “Hello World” program in Eclipse)
  • 为什么十六进制地址是14个字符?(why the hex address is 14 character?)
  • 如何在Python中的不同类中使用变量?(How to use variable in different classes in Python?)
  • asp:GridView HYPERLINKFIELD - datanavigateurlformatstring中的asp代码(asp:GridView HYPERLINKFIELD - asp code inside datanavigateurlformatstring)
  • 关于adaboost算法(About adaboost algorithm)
  • 在Matlab上内置图像(Built in Images on Matlab)
  • java swing:输入键事件时焦点丢失(java swing: Focus lost on enter key event)
  • C#中的通用约束,T是相同的TSomethingElse,对吧?(Generic constraints in C#, T is the same TSomethingElse, right?)
  • 从mybatis中的光标获取数据(Fetching data from cursor in mybatis)
  • 在运行时从XML构建对象的最佳方法(Best way for building objects out of XMLs at runtime)
  • 在整个窗口中拉伸sf :: Sprite(Stretch sf::Sprite across entire window)
  • Selenium Node API Web驱动程序等待超时处理程序(Selenium Node API web driver wait timeout handler)
  • 函数使用并且是map的一部分(循环依赖?)(Function uses and is part of map (circular dependency?))
  • 是否可以在C ++ 14中使用可选模板参数创建类型元组?(Is possible to make a tuple of types with optional template parameters in C++14?)
  • PHP从窗帘后面打印/ f(PHP prints /f from behind the curtains)
  • JFrame的contentPane的LayoutManager(LayoutManager of JFrame's contentPane)
  • 用于Instagram Feed的图像滚动(Image Roll Overs for Instagram Feed)
  • 如何显示拉伸字体(双倍宽度/高度)?(How to display stretched font (double width/height)?)
  • 文件操作API在fileapi.h和stdio.h中的WP8差异?(WP8 differences between file manipulation APIs in fileapi.h and stdio.h?)
  • 保存失败后Rails没有回滚事务()(Rails not rolling back transaction after failed save())
  • jqgrid中的分页问题与数组数据(Pagination problem in jqgrid with array data)
  • 重定向时,通过其他页面上的URL调用javascript函数(Call javascript function through url on otherpage while Redirecting)
  • 如何使用InvokeCommandAction调用我的方法并传入参数?(How do I go about using InvokeCommandAction to call a method of mine and pass in parameters?)
  • Jquerymobile按钮仅显示页面的第一个外观(Jquerymobile buttons are shown only first apperance of the page)