Syntax error, unexpected kELSE

I've been trying to solve the problem in the following bit of code for
quite some time now with no luck. The unexpected kELSE error is occuring
in the commented line. $map is a multi-dimensional array and the
makeshift iterator works properly without the if/else statement. Any
ideas?

def pmap
    input = gets.downcase.chomp
    if input == "exit"
  $plrObj.save
  puts "Exiting..."
  $running = false
    else
  buf = ""
  z = 0
  $map.each do |x|
      x.each do |y|
    if z < 5
        buf = buf + y.to_s
        z++
    else
        buf = buf + "\n"
        z = 0
    end
      end
  end
  puts buf
    end
end

···

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

Hi --

···

On Wed, 27 Aug 2008, Chris Bailey wrote:

I've been trying to solve the problem in the following bit of code for
quite some time now with no luck. The unexpected kELSE error is occuring
in the commented line. $map is a multi-dimensional array and the
makeshift iterator works properly without the if/else statement. Any
ideas?

def pmap
   input = gets.downcase.chomp
   if input == "exit"
$plrObj.save
puts "Exiting..."
$running = false
   else
buf = ""
z = 0
$map.each do |x|
     x.each do |y|
   if z < 5
       buf = buf + y.to_s
       z++

There's no ++ increment operator. Change that to z += 1.

David

--
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
   Advancing with Rails January 19-22 Fort Lauderdale, FL
See http://www.rubypal.com for details and updates!

I've been trying to solve the problem in the following bit of code for
quite some time now with no luck. The unexpected kELSE error is occuring
in the commented line. $map is a multi-dimensional array and the
makeshift iterator works properly without the if/else statement. Any
ideas?

def pmap
   input = gets.downcase.chomp
   if input == "exit"
$plrObj.save
puts "Exiting..."
$running = false
   else
buf = ""
z = 0
$map.each do |x|
     x.each do |y|
   if z < 5
       buf = buf + y.to_s
       z++

Ruby doesn't support this syntax. Change it to z += 1.

   else
       buf = buf + "\n"
       z = 0
   end
     end
end
puts buf
   end
end

Hope this helps,

Jesus.

···

On Wed, Aug 27, 2008 at 2:55 PM, Chris Bailey <christopher.sean.bailey@gmail.com> wrote:

Apart from the ++ issue you might want to change other things as well. First of all, you can use "buf += y.to_s" but this is less efficient than "buf << y.to_s".

Then, you are skipping every fifth element. Is this really what you want?

Finally, if you are printing to console anyway direct printing is more efficient. So I am assuming that you probably rather want something like this:

def pmap
   input = gets.downcase.chomp
   if input == "exit"
     $plrObj.save
     puts "Exiting..."
     $running = false
   else
     z = 0
     $map.each do |x|
       x.each do |y|
  print y

  if z >= 5
    puts
    z = 0
  end
       end
     end
     puts unless z == 0
   end
end

Kind regards

  robert

···

On 27.08.2008 14:55, Chris Bailey wrote:

I've been trying to solve the problem in the following bit of code for
quite some time now with no luck. The unexpected kELSE error is occuring
in the commented line. $map is a multi-dimensional array and the
makeshift iterator works properly without the if/else statement. Any
ideas?

def pmap
    input = gets.downcase.chomp
    if input == "exit"
  $plrObj.save
  puts "Exiting..."
  $running = false
    else
  buf = ""
  z = 0
  $map.each do |x|
      x.each do |y|
    if z < 5
        buf = buf + y.to_s
        z++
    else
        buf = buf + "\n"
        z = 0
    end
      end
  end
  puts buf
    end
end

Jesús Gabriel y Galán wrote:

···

On Wed, Aug 27, 2008 at 2:55 PM, Chris Bailey > <christopher.sean.bailey@gmail.com> wrote:

puts "Exiting..."
$running = false
   else
buf = ""
z = 0
$map.each do |x|
     x.each do |y|
   if z < 5
       buf = buf + y.to_s
       z++

Ruby doesn't support this syntax. Change it to z += 1.

   else
       buf = buf + "\n"
       z = 0
   end
     end
end
puts buf
   end
end

Hope this helps,

Jesus.

Thanks a million guys, I go back and forth with languages and didn't
even notice that. I wish the error had been "Syntax Error, You are an
idiot". I probably would have figured it out faster.
--
Posted via http://www.ruby-forum.com/\.

Hi --

···

On Wed, 27 Aug 2008, Chris Bailey wrote:

Jesús Gabriel y Galán wrote:

On Wed, Aug 27, 2008 at 2:55 PM, Chris Bailey >> <christopher.sean.bailey@gmail.com> wrote:

puts "Exiting..."
$running = false
   else
buf = ""
z = 0
$map.each do |x|
     x.each do |y|
   if z < 5
       buf = buf + y.to_s
       z++

Ruby doesn't support this syntax. Change it to z += 1.

   else
       buf = buf + "\n"
       z = 0
   end
     end
end
puts buf
   end
end

Hope this helps,

Jesus.

Thanks a million guys, I go back and forth with languages and didn't
even notice that. I wish the error had been "Syntax Error, You are an
idiot". I probably would have figured it out faster.

Strictly speaking it's not a syntax error. You can do:

   a++
   b

and it's the same as a + +b.

David

--
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
   Advancing with Rails January 19-22 Fort Lauderdale, FL
See http://www.rubypal.com for details and updates!

David A. Black wrote:

Strictly speaking it's not a syntax error.

As the ++ and -- notations are so common in other languages, and some of
us speak several of those languages, might it be helpful to have a low
level warning issued by Ruby?

···

--
Posted via http://www.ruby-forum.com/\.

Hi --

···

On Thu, 28 Aug 2008, Dave Bass wrote:

David A. Black wrote:

Strictly speaking it's not a syntax error.

As the ++ and -- notations are so common in other languages, and some of
us speak several of those languages, might it be helpful to have a low
level warning issued by Ruby?

I don't think so. I prefer not to have Ruby apologize for not being
Perl or C :slight_smile: It's better to save warnings for things that have to do
with Ruby itself. Also, learning about += is a kind of rite of passage
:slight_smile:

David

--
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
   Advancing with Rails January 19-22 Fort Lauderdale, FL *
   * Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!