syntax error, unexpected '}', expecting kEND

Discussion in 'Ruby' started by Louise Rains, Aug 11, 2010.

  1. Louise Rains

    Louise Rains Guest

    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] += 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/.
     
    Louise Rains, Aug 11, 2010
    #1
    1. Advertising

  2. > 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/.
     
    Brian Candler, Aug 11, 2010
    #2
    1. Advertising

  3. 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
     
    David A. Black, Aug 11, 2010
    #3
  4. 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 }
     
    Joel VanderWerf, Aug 11, 2010
    #4
  5. Louise Rains

    Louise Rains Guest

    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/.
     
    Louise Rains, Aug 11, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Slain Wilde
    Replies:
    1
    Views:
    117
    Gerald Murray
    Aug 9, 2006
  2. Raju Gautam
    Replies:
    1
    Views:
    144
    Robert Klemme
    Oct 29, 2006
  3. Comfort Eagle

    unexpected kEND, expecting $

    Comfort Eagle, Nov 23, 2006, in forum: Ruby
    Replies:
    2
    Views:
    103
    Jan Svitok
    Nov 23, 2006
  4. thufir
    Replies:
    2
    Views:
    547
    thufir
    Apr 21, 2008
  5. Alec Williams
    Replies:
    2
    Views:
    315
    Stefano Crocco
    Jul 19, 2009
Loading...

Share This Page