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.
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?
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
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`
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?