0

How do I iterate through an array if I have the specific index?

@lv = {'apple' => ['round', 'red'], 'name' => ['tags', 'more tags']} if params[:value] @lv.each do |key, tags| if params[:value] == key tags.each_with_index do |tag, index| ... should display round and red? end end end end 

I have an array @lv and I want to be able to get the values if there's a parameter associated with it. example:

someURL.com/?value=0 Then this is supposed to get the key apple. I want to get the values from apple which should be round and red. My logic in the above codes is wrong, but I'm trying to figure out what is the syntax to call out the correct key to iterate?

4
  • 2
    This makes no sense; @lv is a map, not an array. Using numeric indices on a map is strange. Commented Apr 14, 2013 at 20:06
  • @DaveNewton I'm new to rails, and it just seems to be working when I use it as a variable or some sort, is it better to just take the @ out? Commented Apr 14, 2013 at 20:07
  • Whether it's an instance or local variable is not relevant to the question. Commented Apr 14, 2013 at 20:08
  • 1
    Hi Andrew, please tell me what you are trying to achieve and I can suggest an alternate solution. How are you generating @lv, and why... and why are you passing an index in the URL rather than the actual desired key (ie. apple) Commented Apr 14, 2013 at 20:14

2 Answers 2

1

This makes no sense; @lv is a map, not an array. Using numeric indices on a map is strange.

You can rely on insertion order in Ruby 1.9+, so @lv[@lv.keys[params[:value].to_i]] would actually retrieve what you want, but IMO it's semantically sketchy.

@lv[@lv.keys[params[:value].to_i]].each_with_index do |tag, index| ... end 

I'd also recommend using at least one intermediate variable to clean up that expression.

Sign up to request clarification or add additional context in comments.

4 Comments

would be able to give an alternative example?
@andrewliu Doing it "right" depends on what you're actually doing, and why, which I don't know.
Can you explain what your code does? I'm sorry, I'm really new to Ruby on Rails
@andrewliu Break it down expression by expression: it uses the integer value of the parameter as an array index into the keys of the hash to get a key, then uses that key to get a value from the hash, then iterates over the hash value, assumed to be an array.
1

This seems like a lot of effort to make it work this way. Why not change the view so you can select the key name:

http://example.com/?value=apple instead of http://example.com?value=0

then you can change your controller code to:

@tags = @lv[params['value']] 

Then just iterate through @tags in your view, or whatever else you're trying to do with the list of tags.

1 Comment

I tried testing puts @lv[params['apple']] but doesn't come out anything nor did puts @lv[params[0]]. What is the correct syntax to getting something similar to params[0]? I'd like to output the value based on the index. So if I want to get values from apple, I would need to pass 0 in somewhere. How do I do that?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.