Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > syntax error, unexpected '}', expecting kEND

Reply
Thread Tools

syntax error, unexpected '}', expecting kEND

 
 
Louise Rains
Guest
Posts: n/a
 
      08-11-2010
I'm using ruby 1.8.6 (2010-02-04 patchlevel 39 [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/.

 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      08-11-2010
> 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/.

 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      08-11-2010
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

 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      08-11-2010
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 }

 
Reply With Quote
 
Louise Rains
Guest
Posts: n/a
 
      08-11-2010
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
genie.rb:20: syntax error, unexpected $end, expecting kEND Alec Williams Ruby 2 07-19-2009 07:30 AM
rakefile gives syntax error, unexpected ':', expecting $end thufir Ruby 2 04-21-2008 03:50 AM
unexpected kEND, expecting $ Comfort Eagle Ruby 2 11-23-2006 10:32 PM
parse error, unexpected $, expecting kEND instantrails Raju Gautam Ruby 1 10-29-2006 11:31 AM
parse error, unexpected $, expecting kEND Slain Wilde Ruby 1 08-09-2006 03:34 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57