Syntax Error

Hi All,

I am somewhat new to Ruby coding and programming altogether,

puts "enter Product ID Number"
product_id = gets.chomp
CSV.foreach("c:\plantproducts.csv") do |row|
if product_id == row[0]
do (price * qty)
end
end

puts "Enter Qty)
qty = gets.chomp

# the price is given in the csv file when I run the code I get an error
"syntax error, unexpected keyword_do_block do (qty * price)

Can someone give me some advice so that shit code runs?

# The CSV file has product_id, name, price , then type....if this helps
at all.

thanks

···

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

The do keyword in the line after the if is wrong. Remove it and everything
should work, at least after you also add the missing quote at the end of the
line before the last.

I hope this helps

Stefano

···

On Sunday 13 November 2011 06:51:16 Darren H. wrote:

Hi All,

I am somewhat new to Ruby coding and programming altogether,

puts "enter Product ID Number"
product_id = gets.chomp
CSV.foreach("c:\plantproducts.csv") do |row|
if product_id == row[0]
do (price * qty)
end
end

puts "Enter Qty)
qty = gets.chomp

# the price is given in the csv file when I run the code I get an error
"syntax error, unexpected keyword_do_block do (qty * price)

Can someone give me some advice so that shit code runs?

# The CSV file has product_id, name, price , then type....if this helps
at all.

thanks

Since I remove the do line, how do I get the output of price which is
located in the CSV file under price by just having a user enter the
product_id?

···

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

test.rb:14:in `block in <main>': undefined local variable or method
`price' for
main:Object (NameError)
        from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1768:in `each'
        from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1202:in `block in foreach'
        from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1340:in `open'
        from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1201:in `foreach'
        from test.rb:12:in `<main>'
#error i have...

#new code
puts "Enter Product ID Number"
product_id = gets.chomp
CSV.foreach("c:\plantproducts.csv") do |row|
if product_id == row[0]
(price*qty) == subtotal
puts "Enter Quantity"
quantity = gets.chomp

end
end
subtotal = (price * quantity)
puts subtotal

I don't understand why price is an unidentified variable... is the
current code not reading it in the CSV file and giving it as an output?

···

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

Are you saying that I should add

- if price == row[2] # because price is in row 2?

Also but the quantity is not located in the CSV file it will be entered
by the customer after they enter the product_id of the plant they want.

Sorry I am slow and not very good with ruby thanks for your time and
patience.

···

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

hi Darren,

puts "enter Product ID Number"
product_id = gets.chomp
CSV.foreach("c:\plantproducts.csv") do |row|
if product_id == row[0]
do (price * qty)
end
end

puts "Enter Qty)
qty = gets.chomp

# The CSV file has product_id, name, price , then type....if this helps
at all.

  neither `price` nor `qty' are yet defined when you try to multiply
them, try something like this:

  require 'csv'

  puts "enter Product ID Number: "
  product_id = gets.chomp

  puts "Enter Qty: "
  qty = gets.chomp.to_f #convert string input to a float with `to_f`

  CSV.foreach("c:\plantproducts.csv") do |row|
    if product_id == row[0]
      price = row[2].to_f # fetch, convert from string, and store in
`price variable`
      total = (price * qty)
      puts "your total is: #{total}"
    end
  end

  hth,

  - j

···

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

J-

This is an error i get now,

C:\>ruby test1.rb
enter Product ID Number:
1001
Enter Qty:
2
test1.rb:12:in ``': No such file or directory - price variable
(Errno::ENOENT)
        from test1.rb:12:in `block in <main>'
        from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1768:in `each'
        from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1202:in `block in foreach'
        from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1340:in `open'
        from C:/Ruby192/lib/ruby/1.9.1/csv.rb:1201:in `foreach'
        from test1.rb:9:in `<main>'

C:\>

The price column is the 3rd one so it should be
-price = row[2].to_f # fetch, convert from string, and store in
`price variable`

Thanks for the help I really appreciate it.

-Darren

···

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

-----Messaggio originale-----

···

Da: Darren H. [mailto:dhulem1@umbc.edu]
Inviato: sabato 12 novembre 2011 22:51
A: ruby-talk ML
Oggetto: Syntax Error

Hi All,

I am somewhat new to Ruby coding and programming altogether,

puts "enter Product ID Number"
product_id = gets.chomp
CSV.foreach("c:\plantproducts.csv") do |row| if product_id == row[0] do
(price * qty) end end

puts "Enter Qty)
qty = gets.chomp

# the price is given in the csv file when I run the code I get an error
"syntax error, unexpected keyword_do_block do (qty * price)

Can someone give me some advice so that shit code runs?

# The CSV file has product_id, name, price , then type....if this helps at
all.

thanks

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

--
Caselle da 1GB, trasmetti allegati fino a 3GB e in piu' IMAP, POP3 e SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f

Sponsor:
Conto Arancio al 4,20%. Zero spese e massima liberta', aprilo in due minuti!
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=11919&d=29-12

You don't need to remove the line with the do, just the do keyword:

puts "enter Product ID Number"
product_id = gets.chomp
CSV.foreach("c:\plantproducts.csv") do |row|
if product_id == row[0]
   (price * qty) #The do is gone
end
end

puts "Enter Qty)"
qty = gets.chomp

You won't be able to do anything with the result of price*qty, however, unless
you store it in a variable.

Stefano

···

On Sunday 13 November 2011 07:44:16 Darren H. wrote:

Since I remove the do line, how do I get the output of price which is
located in the CSV file under price by just having a user enter the
product_id?

...

if product_id == row[0]
(price*qty) == subtotal

...

I don't understand why price is an unidentified variable... is the
current code not reading it in the CSV file and giving it as an output?

You accessed the product id in the CSV by indexing into the row (row[0])
Why do expect price and qty to be different?

cheers

The line:

price = row[2].to_f # fetch, convert from string, and store in
`price variable`

should be a single line but has probably been wrapped by thew mail
program. Either join the two lines together or just delete 'price
variable'.

WOW I my notepad++ wrapped text so that 'price variable' was on separate
line not commented out. It worked thanks all for the hep

···

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

-----Messaggio originale-----

···

Da: Peter Hickman [mailto:peterhickman386@googlemail.com]
Inviato: domenica 13 novembre 2011 18:28
A: ruby-talk ML
Oggetto: Re: Syntax Error

The line:

price = row[2].to_f # fetch, convert from string, and store in `price
variable`

should be a single line but has probably been wrapped by thew mail program.
Either join the two lines together or just delete 'price variable'.

--
Caselle da 1GB, trasmetti allegati fino a 3GB e in piu' IMAP, POP3 e SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f

Sponsor:
Conto Arancio al 4,20%. Zero spese e massima liberta', aprilo in due minuti!
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=11919&d=29-12