Reading CSV File

Hi all

I am new to ruby and need a little help for a project.

My code so far is…

require ‘csv’
CSV.foreach(“c:\plantproducts.csv”) do |row|
puts row.join(",")
end

puts “enter Product ID Number”
product_id = gets.chomp
CSV.foreach(“c:\plantproducts.csv”) do |row|
product_id == row[0]
end

*In my csv file the columns go as follows Product id, name, price, type

my question is when the program is running and when you enter in your
product id number as it matches in the csv file (such as 1001 and the
price is 8.00) how do I get the other data from the csv file such as
price so I can later use it and show to the program user)

Thanks

On Thu, Nov 10, 2011 at 20:23, Darren H. [email protected] wrote:

require ‘csv’
CSV.foreach(“c:\plantproducts.csv”) do |row|
puts row.join(",")
end

Why do this? It just joins together what CSV has put asunder. Or are
you just looking for confirmation of the file contents?

puts “enter Product ID Number”
product_id = gets.chomp
CSV.foreach(“c:\plantproducts.csv”) do |row|
product_id == row[0]
end

You’re looping over the entire file… and only comparing to the
desired product_id. The fact that you’re continuing to look, after
you’ve found it, is just an efficiency issue, but mainly you’re not
doing anything based on the result of that comparison. This seems
to be the whole key to your question:

my question is when the program is running and when you enter in your
product id number as it matches in the csv file (such as 1001 and the
price is 8.00) how do I get the other data from the csv file such as
price so I can later use it and show to the program user)

You’ve still got the row array there. All you’ve got to do is use it.
Something like:

CSV.foreach(“c:\plantproducts.csv”) do |row|
if product_id == row[0]
do something with row
i also suggest you exit the loop
end
end

Exactly what you should do with row, and how to exit the loop, I’ll
leave as exercises.

-Dave

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)

This is the output of the error
C:>ruby test.rb
test.rb:14: syntax error, unexpected keyword_do_block
do (price * quantity)
^

What I am trying to do is:

-Get the users input for product_id
-Get Users input for quantity
-Lookup in the CSV file (which contains product_id,name,price,and type)
to match the users input for product_id and match it with price
-Then multiply price (from the CSV file) times quantity which the user
gives and produce an output.

–Darren

On Thu, Nov 10, 2011 at 21:38, Darren H. [email protected] wrote:

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)

Does the code that gives you the error, look like the above, though
minus the quote markers of course? I very much doubt it, since at the
very least the puts of the quantity prompt, is not syntactically
valid. If you try to retype, you’re bound to get something wrong,
assuming you are in fact only human. :slight_smile: Make the machine do the
work, and use copy-and-paste.

Now, assuming it at least resembles the above, you’ve got something
weird going on in the stuff you tell it to do in case of a match.
What is “do (price * qty)” supposed to mean? At that point, the user
hasn’t even entered a quantity, so you’ll probably wind up with it
gritching about an undefined variable, or if it somehow defaults to
zero the math result will always be zero, or if its value is leftover
from a previous run then it’s likely to be wrong for this one.

As for the unexpected keyword, I think what it’s probably trying to
tell you is that it doesn’t expect a “do” at that point. A “do” is
usually for passing a block of code to some method such as map or
times, to tell it what to map onto each item or what to do a given
number of times. But you don’t have any such construct preceding your
“do”.

Since I’m not sure exactly what you’re trying to accomplish and how
(though I think I could make a good guess), here’s what I suggest.
Start with plain English (or whatever). Write out how you would tell
a human to do what you’re asking. Do it in very simple steps, one
by one. Indent sub-steps, ending indented blocks with “end” or some
such placeholder just to be sure. (I’m going to use dash-space pairs
just so they don’t get squished out in the web forum, but “for real”
I’d use just spaces.) Then translate each step into Ruby. If you
find that something doesn’t translate well, you probably need to break
it down into simpler steps. (These can be either put where they are
in the list, or as a new method.) This is what we call “pseudocode”.
Producing these thoughts, if not necessarily this writing, is in
fact the core essential of old-style programming, what the fancypants
call “top-down stepwise refinement”. It’s also the core of modern
programming once you get past the overall system design of objects and
suchlike, certainly applicable at the level of individual methods.
Once you get very well versed in this sort of thing, you can skip this
step entirely and spit out code directly.

In the case of what I think you’re trying to do, it might start like:

  • Ask me for a Product ID Number
  • Write down what I tell you, and mark it as ID
  • Look up the information about the product with that ID
  • Ask me for a Quantity
  • Write down what I tell you, and mark it as Quantity
  • Now what do we do? (The code above stops there.)

Then “Look up the information about the product with that ID” might
then get broken up into:

  • For each line in the CSV file
    • If the first part of the line matches the ID you wrote down
      • This seems to be the part where you’re not sure what to do, right?
    • end
  • end

Can you take it from there?

-Dave

-----Messaggio originale-----
Da: Darren H. [mailto:[email protected]]
Inviato: sabato 12 novembre 2011 23:07
A: ruby-talk ML
Oggetto: Re: Reading CSV File

This is the output of the error
C:>ruby test.rb
test.rb:14: syntax error, unexpected keyword_do_block do (price *
quantity)
^

What I am trying to do is:

-Get the users input for product_id
-Get Users input for quantity
-Lookup in the CSV file (which contains product_id,name,price,and type)
to
match the users input for product_id and match it with price -Then
multiply
price (from the CSV file) times quantity which the user gives and
produce an
output.

–Darren


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:
ING DIRECT Conto Arancio. 4,20% per 12 mesi, zero spese, aprilo in due
minuti!
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=11921&d=29-12

-----Messaggio originale-----
Da: Darren H. [mailto:[email protected]]
Inviato: venerd 11 novembre 2011 02:24
A: ruby-talk ML
Oggetto: Reading CSV File

Hi all

I am new to ruby and need a little help for a project.

My code so far is…

require ‘csv’
CSV.foreach(“c:\plantproducts.csv”) do |row| puts row.join(",") end

puts “enter Product ID Number”
product_id = gets.chomp
CSV.foreach(“c:\plantproducts.csv”) do |row| product_id == row[0] end

*In my csv file the columns go as follows Product id, name, price, type

my question is when the program is running and when you enter in your
product id number as it matches in the csv file (such as 1001 and the
price
is 8.00) how do I get the other data from the csv file such as price so
I
can later use it and show to the program user)

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:
Capodanno al parco Oltremare Riccione: Pacchetto hotel 3 stelle in
centro + ingresso al parco.

Hi Darren,

The way you use the do block is not appropriate.

Anyway, if I understood your problem properly, here you go:

Read the CSV as like below which stores the whole content in an array of
arrays: (Refer:
http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV.html)

arr_of_arrs = CSV.read(“Sample.csv”)

print "Enter Product ID: "
product_id = gets.chomp

print "Enter Quantity: "
quantity = gets.chomp

puts
arr_of_arrs[1…-1].each do |data|
if data[0] == product_id
puts “Product ID \t : \t#{data[0]}”
puts “Product Name \t : \t#{data[1]}”
puts “Product Price \t : \t#{data[2]}”
puts “Product Type \t : \t#{data[3]}”
puts

puts "Total Price ordered \t : \t#{data[2].to_i * quantity.to_i}"
exit

end
end

I hope this is your requirement. Still you can enhance this by handling
worst scenarios (using recursion).

Cheers,

  • Vimal