Possible to Access the Index in a Hash Each Loop

Possible to access the index in a Hash each loop?

If you like to know Index of each iteration you could use .each_with_index

hash.each_with_index { |(key,value),index| ... }

Best way to iterate a list of hash in ruby

Your example is quite confusing, but the regular way to iterate a hash is the following

hash.each do |key, value|
end

So in your example it looks like you should do

records.each do |account_map| 
account_map.each do |index, array|
array.each do |hash|
hash['account_id'] # This is how you access your data
hash['v'] # This is how you access your data
end
end
end

Of course you should use better variables names than index, array and hash.

Is there a way to iterate through a hash with index values?

There is the Enumerable#each_with_index method, which for a hash would be used like this:

hash.each_with_index { |(k, v), i| ... }

However, you could solve your particular problem using

wrong_hash.keys.drop(1).zip(wrong_hash.values).to_h
# => {:b=>"banana", :c=>"cabbage", :d=>"dental_floss"}

Ruby hash iteration, index access and value mapping

Your code just assigns values to some local variables. You need to assign the new values to the hash itself:

hash.each_with_index do |(key, array), index|
element1, element2 = array
element1 = "new value"
element2 = "new value2"
hash[key] = [element1, element2]
end

Or shorter (depending on what you try to achieve):

hash.each do |key, array|
hash[key] = ["new value", "new value2"]
end

Or:

hash.update(hash) do |key, array|
["new value", "new value2"]
end

How to create a .each loop to grab a key and value from a hash based on user input?

You can use find:

answer = 120
laser = laserType.find { |type, distance| distance == answer }
# => [:xenon_ion, 120]

puts "Try using a #{laser.first.to_s.tr("_", " ")} laser."

If you want to match multiple lasers, instead of find you would use select, which will return an array of all of the matching values:

matches = laserType.select { |_, distance| distance == answer }
puts "Try using one of these: " + matches.map { |type, _| type.to_s.tr("_", " ") }.join(", ")

You can improve this by using a range (10..15) to allow for a range of distances:

laser_types = {
blaster: 190..210,
helium_neon: 170..190,
free_electron: 70..150,
nitrogen: 0..50,
}

laser_types.select { |_, range| range.cover?(answer) }

last element in Hash.each

For example like this:

foo.each_with_index do |elem, index|
if index == foo.length - 1
puts 'this is a last element!'
else
# smth
end
end

The problem you might have is that items in a map are not coming in any specific order. On my version of Ruby I see them in the following order:

["abc", 3]
["foo", 1]
["bar", 2]

Maybe you want to traverse the sorted keys instead. Like this for example:

foo.keys.sort.each_with_index do |key, index|
if index == foo.length - 1
puts 'this is a last element!'
else
p foo[key]
end
end

Is it possible to populate a hash using a `for` loop?

for loops are rarely used. I would suggest each:

hash = {}
(1..18).each { |i| hash[i] = 'free' }
hash
#=> { 1=>"free", 2=>"free", 3=>"free", 4=>"free", 5=>"free", 6=>"free",
# 7=>"free", 8=>"free", 9=>"free", 10=>"free", 11=>"free", 12=>"free",
# 13=>"free", 14=>"free", 15=>"free", 16=>"free", 17=>"free", 18=>"free"}

You can also pass the initial object - i.e. the empty hash - into the loop using each_with_object:

hash = (1..18).each_with_object({}) { |i, h| h[i] = 'free' }

Within the block, the second argument h refers to the passed-in hash, so it can be modified. At the end, each_with_object returns the filled hash.

Another option is to use map:

hash = (1..18).map { |i| [i, 'free'] }.to_h

This builds an intermediate array [[1, 'free'], [2, 'free'], ...] and converts it to a hash afterwards using Array#to_h.

How to iterate over an array of hashes in Ruby to compare each member with the following members in the array?

Note that you're not using "i" inside your loop, which looks like a bug. Also, your index "j+1" is going off the end of the array, resulting in accessing the nil element. Actually, even "j" goes off the end of the array. Arrays are accessed from 0...length-1, whereas "0..data.length" will access the element at index data.length. I'm guessing you mean something more like:

for i in 0..data.length-2
for j in i+1..data.length-1
output = (data[i].keys & data[j].keys).select { |k| data[i][k] == data[j][k] }
end
end

Automatically get loop index in foreach loop in Perl

Like codehead said, you'd have to iterate over the array indices instead of its elements. I prefer this variant over the C-style for loop:

for my $i (0 .. $#x) {
print "$i: $x[$i]\n";
}


Related Topics



Leave a reply



Submit