From: Louise Rains on
I'm using ruby 1.8.6 (2010-02-04 patchlevel 398) [i386-mingw32], WinXP
and Notepad++ in the Unix mode (for line endings)

I have the following code:

q = [1.0, 4.0, 10.0, 27.0, 50.0, 600.0, 1000.0, 5833.0, 6250.0,
1500000.0]
x = q.each{|e| print Math.log10(e).to_i, " "}
p x
r = q.collect{|e| Math.log10(e).to_i}
p r
q.inject(Array.new(4,0)){ |h,e| if e < 10000 then h[Math.log10(e).to_i]
+=1 }

i=0
q.inject(Array.new(4,0)) do |h,e|
if e < 10000 then h[r[i]] += 1
i += 1
end

The first 5 lines work as expected. However, the first q.inject line
and the q.inject block give the following errors:

D:\abc>ruby doh2.rb
doh2.rb:6: syntax error, unexpected '}', expecting kEND
doh2.rb:12: syntax error, unexpected $end, expecting kEND


My goal is to take the numbers in q and count how many are in the ranges
1-9, 10-99, 100-999 and 1000-9999.

I thought maybe it was using the Math.log10 expression as an array
index, hence the second q.inject code.

I have looked at an octal dump. No surprises there.

0000000 q = [ 1 . 0 , 4 . 0 , 1
0000020 0 . 0 , 2 7 . 0 , 5 0 . 0 ,
0000040 6 0 0 . 0 , 1 0 0 0 . 0 ,
0000060 5 8 3 3 . 0 , 6 2 5 0 . 0 ,
0000100 1 5 0 0 0 0 0 . 0 ] \n x = q
0000120 . e a c h { | e | p r i n t
0000140 M a t h . l o g 1 0 ( e ) . t o
0000160 _ i , " " } \n p x \n r =
0000200 q . c o l l e c t { | e | M
0000220 a t h . l o g 1 0 ( e ) . t o _
0000240 i } \n p r \n q . i n j e c t (
0000260 A r r a y . n e w ( 4 , 0 ) ) {
0000300 | h , e | i f e < 1 0
0000320 0 0 0 t h e n h [ M a t h .
0000340 l o g 1 0 ( e ) . t o _ i ] +
0000360 = 1 } \n \n i = 0 \n q . i n j e
0000400 c t ( A r r a y . n e w ( 4 , 0
0000420 ) ) d o | h , e | \n \t i f
0000440 e < 1 0 0 0 0 t h e n h
0000460 [ r [ i ] ] + = 1 \n \t i +
0000500 = 1 \n e n d \n
0000510

Any ideas as to what is going on here? I have also tried other inject
statements that I know used to work, but they don't now. Is inject
broken in my version?

Thanks,
Louise
--
Posted via http://www.ruby-forum.com/.

From: Brian Candler on
> q.inject(Array.new(4,0)){ |h,e| if e < 10000 then h[Math.log10(e).to_i]
> +=1 }

"if" needs a matching "end", unless you use the postfix form:

h[...] += 1 if e < 10000

BTW, even if you're using ruby 1.8.7, running ruby 1.9.x on your code
with the -w flag can sometimes give better error messages.
--
Posted via http://www.ruby-forum.com/.

From: David A. Black on
Hi --

On Thu, 12 Aug 2010, Louise Rains wrote:

> q.inject(Array.new(4,0)){ |h,e| if e < 10000 then h[Math.log10(e).to_i] +=1 }

There's no 'end' on that 'if'.


David

--
David A. Black, Senior Developer, Cyrus Innovation Inc.

The Ruby training with Black/Brown/McAnally
Compleat Philadelphia, PA, October 1-2, 2010
Rubyist http://www.compleatrubyist.com

From: Joel VanderWerf on
Louise Rains wrote:
> q.inject(Array.new(4,0)){ |h,e| if e < 10000 then h[Math.log10(e).to_i]
> +=1 }

You need an "end" to go with the "if .. then .. " :

q.inject(Array.new(4,0)){ |h,e| if e < 10000 then h[Math.log10(e).to_i]
+=1; end }

From: Louise Rains on
Brian Candler wrote:
>> q.inject(Array.new(4,0)){ |h,e| if e < 10000 then h[Math.log10(e).to_i]
>> +=1 }
>
> "if" needs a matching "end", unless you use the postfix form:
>
> h[...] += 1 if e < 10000
>
> BTW, even if you're using ruby 1.8.7, running ruby 1.9.x on your code
> with the -w flag can sometimes give better error messages.

Thanks, that is just the ticket!
--
Posted via http://www.ruby-forum.com/.