[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Array : each_with_index

raja

7/19/2007 6:46:00 AM

Hi All,

I have following lines


m=['a','b','c']
puts m.each_with_index{|v,i| i}

which output in:

>ruby try.rb
a
b
c
>Exit code: 0



I was expecting to see indexes, rather than values.
Am I overlooking some trivial nuance or what.
Basically i wanted to print index values while iterating an array at
some other project.
and "each_with_index" seemed very obvious for the task but for the
unexpected result.

Raja

--
Posted via http://www.ruby-....

13 Answers

Robert Klemme

7/19/2007 6:51:00 AM

0

2007/7/19, Vin Raja <vineetraja@gmail.com>:
> Hi All,
>
> I have following lines
>
>
> m=['a','b','c']
> puts m.each_with_index{|v,i| i}
>
> which output in:
>
> >ruby try.rb
> a
> b
> c
> >Exit code: 0
>
>
>
> I was expecting to see indexes, rather than values.
> Am I overlooking some trivial nuance or what.
> Basically i wanted to print index values while iterating an array at
> some other project.
> and "each_with_index" seemed very obvious for the task but for the
> unexpected result.

It is. But #each_with_index *returns* the Enumerable. You need to put
the put into the block:

irb(main):001:0> ['a','b','c'].each_with_index {|a,i| puts i}
0
1
2
=> ["a", "b", "c"]
irb(main):002:0>

Kind regards

robert

Marcel Molina Jr.

7/19/2007 6:52:00 AM

0

On Thu, Jul 19, 2007 at 03:46:10PM +0900, Vin Raja wrote:
> I have following lines
>
> m=['a','b','c']
> puts m.each_with_index{|v,i| i}
>
> which output in:
>
> >ruby try.rb
> a
> b
> c
> >Exit code: 0

irb(main):003:0> m.each_with_index{|v,i| i}
=> ["a", "b", "c"]
irb(main):004:0> m.each_with_index{|v,i| p i}
0
1
2
=> ["a", "b", "c"]

The i variable in the block is indeed the index. But the result of the entire
each_with_index expression is not the result of each block invocation, but the collection
of all the array elements.

marcel
--
Marcel Molina Jr. <marcel@vernix.org>

Stefano Crocco

7/19/2007 6:53:00 AM

0

Alle giovedì 19 luglio 2007, Vin Raja ha scritto:
> Hi All,
>
> I have following lines
>
>
> m=['a','b','c']
> puts m.each_with_index{|v,i| i}
>
> which output in:
> >ruby try.rb
>
> a
> b
> c
>
> >Exit code: 0
>
> I was expecting to see indexes, rather than values.
> Am I overlooking some trivial nuance or what.
> Basically i wanted to print index values while iterating an array at
> some other project.
> and "each_with_index" seemed very obvious for the task but for the
> unexpected result.
>
> Raja

You're telling ruby to print the return value of each with index, which,
according to the ri documentation, is the array itself. If you want to print
the different inidces, you need to put the call to puts inside the block:

m.each_with_index{|v,i| puts( i )}

Putting i as the last statement of the block simply makes that the return
value of the block, but this is ignored by each_with_index, so it's useless.

I hope this helps

Stefano

raja

7/19/2007 7:12:00 AM

0

Thanks All,

I got the nuance.

But my problem just got a bit more messier.
have a look down here:

r = ['KLP','OGN' ]
msg =<<MSG
Found #{r.length} orders
#{r.map.each_with_index{|v,i| puts "(#{i+1}) #{v}\n" }}
MSG

puts msg

This prints:

>ruby try.rb
(1) KLP
(2) OGN
Found 2 orders
KLPOGN

>Exit code: 0

And this time I also know why, thanks again.

But what I actually wanted was this sort of output:

>ruby try.rb

Found 2 orders
(1) KLP
(2) OGN

>Exit code: 0

Any Help!

Thanks
raja

--
Posted via http://www.ruby-....

Robert Klemme

7/19/2007 8:27:00 AM

0

2007/7/19, Vin Raja <vineetraja@gmail.com>:
> Thanks All,
>
> I got the nuance.
>
> But my problem just got a bit more messier.
> have a look down here:
>
> r = ['KLP','OGN' ]
> msg =<<MSG
> Found #{r.length} orders
> #{r.map.each_with_index{|v,i| puts "(#{i+1}) #{v}\n" }}
> MSG
>
> puts msg
>
> This prints:
>
> >ruby try.rb
> (1) KLP
> (2) OGN
> Found 2 orders
> KLPOGN
>
> >Exit code: 0
>
> And this time I also know why, thanks again.
>
> But what I actually wanted was this sort of output:
>
> >ruby try.rb
>
> Found 2 orders
> (1) KLP
> (2) OGN
>
> >Exit code: 0
>
> Any Help!

You are confusing string evaluation with output. You need to remove
the puts from the string interpolation or use a different approach.
I'd keep things simple and do

r = ['KLP','OGN' ]
print "found ", r.size, " orders\n"
r.each_with_index do |e,i|
print " (", i+1, ") ", e, "\n"
end

robert

Peña, Botp

7/19/2007 9:28:00 AM

0

#... puts "(#{i+1}) #{v}\n" }

oops, lose "\n" pls.

Robert Klemme

7/19/2007 10:58:00 AM

0

2007/7/19, Peña, Botp <botp@delmonte-phil.com>:
> #... puts "(#{i+1}) #{v}\n" }
>
> oops, lose "\n" pls.

It doesn't hurt though - output is the same.

robert

Peña, Botp

7/20/2007 2:23:00 AM

0

From: Robert Klemme [mailto:shortcutter@googlemail.com]
# 2007/7/19, Peña, Botp <botp@delmonte-phil.com>:
# > #... puts "(#{i+1}) #{v}\n" }
# > oops, lose "\n" pls.
#
# It doesn't hurt though - output is the same.

' just little concern for the op. he might assume puts requires \n.

but you're right, it does not hurt. he'll learn it later anyway...

kind regards -botp

raja

7/20/2007 5:42:00 AM

0


Thanks All
It cleared up a lot of mess up at my head.

thanks again
-
Raja




--
Posted via http://www.ruby-....

Brian Adkins

7/20/2007 2:26:00 PM

0

On Jul 19, 3:12 am, Vin Raja <vineetr...@gmail.com> wrote:
> r = ['KLP','OGN' ]
> msg =<<MSG
> Found #{r.length} orders
> #{r.map.each_with_index{|v,i| puts "(#{i+1}) #{v}\n" }}
> MSG
>
> puts msg

It looks like others have already clarified things for you. This is a
bit of a tangent, but I just wanted to mention that 'map' is
superfluous in the above code i.e. the output is identical with and
without it.

irb(main):001:0> r = ['KLP','OGN' ]
=> ["KLP", "OGN"]
irb(main):002:0> r
=> ["KLP", "OGN"]
irb(main):003:0> r.map
=> ["KLP", "OGN"]