17 Mar, 2010, Deimos wrote in the 1st comment:
Votes: 0
Ugh.


$foo = array ( "a", "b", "c", "d", "e" );
// This is what I want to do!
for ( $i = 1; $i <= 3; ++$i )
{
echo "Index ", $i, " is ", $foo[$i], "\n";
}


foo = [ "a", "b", "c", "d", "e" ]
# This doesn't work, since it seems Ruby's ranges
# get re-indexed…
foo[1..3].each_index do |i|
puts "Index #{i} is #{foo[i]}\n" # i is relative to the range :(
end


I can't seem to find what I'm looking for sifting through the Array docs, and my Google-fu has failed me. I throw myself upon your mercy!
17 Mar, 2010, JohnnyStarr wrote in the 2nd comment:
Votes: 0
foo.each {|i| p "Index #{i} is #{foo[i..i]}\n"} # Ruby 1.8.6 anyway
17 Mar, 2010, Deimos wrote in the 3rd comment:
Votes: 0
That loops through every element. I need to loop through only a subset.

Edit: In fact, what I wrote actually loops the correct elements, it's just that "i" is relative to the range (0, 1, 2) instead of to foo (1, 2, 3). I need something that will give me the latter.
17 Mar, 2010, JohnnyStarr wrote in the 4th comment:
Votes: 0
Oh, what about
foo[1..3].each_index do |i|
puts "Index #{i} is #{foo[i..i]}\n" # use [i..i]
end


EDIT: Ok i get it now, can you provide more context? As in, why are you doing things this way?
17 Mar, 2010, Deimos wrote in the 5th comment:
Votes: 0
Newp. "i" is giving me the wrong indexes to begin with, so that won't work either (it would just be 0..0, 1..1, 2..2). I really need "i" to be giving me 1, 2, 3.
17 Mar, 2010, JohnnyStarr wrote in the 6th comment:
Votes: 0
Have you considered a generic for loop?

for i in 1..3
p i
end

# output
# 1
# 2
# 3
17 Mar, 2010, Deimos wrote in the 7th comment:
Votes: 0
I mean, I could do something convoluted and hackish like adding the offset to "i" at the beginning of the loop:


foo = [ "a", "b", "c", "d", "e" ]
start = 1
end = 3
foo[start..end].each_index do |i|
i += start # now i is relative to foo instead of the range
end


But, I'd really like to find something less… stupid. :tongue:
17 Mar, 2010, JohnnyStarr wrote in the 8th comment:
Votes: 0
Ninja
17 Mar, 2010, JohnnyStarr wrote in the 9th comment:
Votes: 0
irb(main):003:0> for i in 1..3
irb(main):004:1> p i
irb(main):005:1> end
1
2
3
=> 1..3
irb(main):006:0>


SO…

for i in 1..3
puts "Index #{i} is #{foo
irb(main):003:0> for i in 1..3
irb(main):004:1> p i
irb(main):005:1> end
1
2
3
=> 1..3
irb(main):006:0>


SO…

for i in 1..3
puts "Index #{i} is #{foo[i}\n"
end
[/code]
17 Mar, 2010, David Haley wrote in the 10th comment:
Votes: 0
EDIT: sigh, ninja'd

Deimos said:
I throw myself upon your mercy!

Well, that was your first mistake… :devil:

Anyhow, try:
$ cat test.rb 

(1..3).each do |i|
puts i
end

$ ruby test.rb
1
2
3
$



Or,

$ cat test.rb 

for i in (1..3)
puts i
end

$ ruby test.rb
1
2
3
$


Maybe one of the Ruby priests will have better magic than me, I am after all just a passing pilgrim from another temple. :tongue:
17 Mar, 2010, Deimos wrote in the 11th comment:
Votes: 0
Hah, yeah. I'm quite embarrassed that I didn't know Ruby had a for..in loop. Thanks.
17 Mar, 2010, Deimos wrote in the 12th comment:
Votes: 0
David Haley said:
(1..3).each do |i|
puts i
end


Ahhhh.. you wizard, you! Can't believe I didn't think of that, either.

I blame it on C-syntax brainwashing.
17 Mar, 2010, David Haley wrote in the 13th comment:
Votes: 0
Yeah, getting rid of C-syntax brainwashing is probably one of the best things that happened to me in terms of growth as a programmer. Sapir-Whorf and all that. Really helps you to think about things from a different angle. Took me a while but I'm in a happier place now. :tongue:
17 Mar, 2010, JohnnyStarr wrote in the 14th comment:
Votes: 0
you dont like mine? really?
for i in 1..3
puts "Index #{i} is #{foofor i in 1..3
puts "Index #{i} is #{foo[i}\n"
end[/code]
:stare:
17 Mar, 2010, Deimos wrote in the 15th comment:
Votes: 0
JohnnyStarr said:
you dont like mine? really?

I did. I thanked you up there somewhere ^^^^^^
17 Mar, 2010, JohnnyStarr wrote in the 16th comment:
Votes: 0
oh, hehe, i didnt mean it to sound like that!
I just meant you liked the other better? meaning for i in 1..3 is more aesthetically pleasing :cool:
17 Mar, 2010, Deimos wrote in the 17th comment:
Votes: 0
Yes, and I actually used the for..in loop. I was just shocked that I hadn't thought about DH's each() solution, since I was pretty much looking at array iterators exclusively.
17 Mar, 2010, JohnnyStarr wrote in the 18th comment:
Votes: 0
gotcha.
yeah, Ruby has a million ways to do things, especially when it comes to loops and iterators (enumerators) etc.
At first I was writing like a C programmer until the whole dynamic typing thing clicked for me. Now I find myself
going back and rewriting little things that vex me here and there. Overall, I've learned most from studying others
coding techniques. I've been amazed at how sometimes I over think things, and other times I notice that I find easier
ways of doing things than others.
17 Mar, 2010, Runter wrote in the 19th comment:
Votes: 0
This is a strange problem you have and I haven't found myself wanting/needing to solve it like this.

You should move away from designing code where index is even important and focus more on the actual values. Instead of trying to have to relate indexes from a newly constructed array to your original array.

Now, if the problem was described like, "Print the first three values from a queue."

a = [1, 2, 3, 4, 5]

a[0..2].each do |v|
p v
end


So maybe you could explain why you need the index relative to the original array.
17 Mar, 2010, Deimos wrote in the 20th comment:
Votes: 0
I'm working on converting my wilderness code, and rooms are indexed in a two-dimensional array by their X,Y coordinates. Not being able to pluck rooms out by their indexes would suck. :tongue:
0.0/36