I don’t know about hh’s implementation but how about
irb(main):003:0> print “foo:”; gets().split
foo:this is a test
=> [“this”, “is”, “a”, “test”]
Also, look at highline at http://highline.rubyforge.org/
Johnny P
— Original Message —
I don’t know about hh’s implementation but how about
irb(main):003:0> print “foo:”; gets().split
foo:this is a test
=> [“this”, “is”, “a”, “test”]
Johnny P
— Original Message —
On 7/26/07, [email protected] [email protected] wrote:
ask command, so you could do things like
Array_2 = ask("put in 2nd vendors prices) and then i could
go on and
subtract the two arraysSorry if this is a totaly idiotic question :(!?
Much thanx for anyones help!Posted via http://www.ruby-forum.com/.
To clarify:
irb(main):006:0> def ask(question)
irb(main):007:1> puts “#{question}:\n”
irb(main):008:1> gets.split
irb(main):009:1> end
=> nil
irb(main):010:0> answer = ask(“Enter 1st vendor’s prices”)
Enter 1st vendor’s prices:
$1 $2 $3 $4
=> [“$1”, “$2”, “$3”, “$4”]
irb(main):011:0> answer
=> [“$1”, “$2”, “$3”, “$4”]
irb(main):012:0>
Although I’d have to point out that ‘ask’ is an awful name for a method.
Chris
Although I’d have to point out that ‘ask’ is an awful name for a method.
I’d say that depends entirely on context. In a context like Hackety
Hack, I think it’s a perfectly good name.
-Gabriel Boyer
Gabe B. wrote:
Although I’d have to point out that ‘ask’ is an awful name for a method.
I’d say that depends entirely on context. In a context like Hackety
Hack, I think it’s a perfectly good name.-Gabriel Boyer
humm ok so i tried out my program but at the very end i get an error…
irb(main):001:0> def ask(question)
irb(main):002:1> puts “#{question}:\n”
irb(main):003:1> gets.split
irb(main):004:1> end
=> nil
irb(main):005:0> Var_1 = ask(“Enter 1st Vendors prices”)
Enter 1st Vendors prices:
14.50 16.50 14
=> [“14.50”, “16.50”, “14”]
irb(main):006:0> Var_2 = ask(“Enter 2nd vendors prices”)
Enter 2nd vendors prices:
13 17.50 14
=> [“13”, “17.50”, “14”]
irb(main):007:0> Var_1.zip(Var_2)
=> [[“14.50”, “13”], [“16.50”, “17.50”], [“14”, “14”]]
irb(main):008:0> Var_1.zip(Var_2).map{ |pair| pair[0] - pair[1] }
NoMethodError: undefined method -' for "14.50":String from (irb):8 from (irb):8:in
map’
from (irb):8
from :0
irb(main):009:0>
could it because im using floats, and not intergers?
On Behalf Of Erik B.:
^^^^ ^^^^
try pair[0].to_f -
pair[1].to_f
so,
irb(main):009:0> Var_1.zip(Var_2).map{ |pair| pair[0].to_f -
pair[1].to_f }
=> [1.5, -1.0, 0.0]
^^^ ^^^^^^
there's the hint
you’re using strings
kind regards -botp
On 7/26/07, Erik B. [email protected] wrote:
irb(main):002:1> puts “#{question}:\n”
=> [“13”, “17.50”, “14”]
Your elements are Strings! That’s what you get back from the gets
method. Let’s change them to Floats. But first, let’s not use
starting capital letters for our variables…
irb> var_1, var_2 = Var_1, Var_2
=> [[“14.50”, “16.40”, 14"], [“13”,“17.50”, “14”]]
irb> [var_1, var_2].each { |array| array.map! { |elem| elem.to_f } }
=> [[14.5, 16.5, 14.0], [13.0, 17.5, 14.0]]
irb(main):007:0> Var_1.zip(Var_2)
You don’t need this ^^^^^^^^ line
irb> difference = var_1.zip(var_2).map { |pair| pair[0] - pair[1] }
=> [1.5, -1.0, 0.0]
Todd
Oh, wow ok should have thought of that, now instead of doing the whole
1.5 and -1.0 thing. It would be neat if it could just print *buy from
vendor #1" or “buy from vendor #2” saying that you always want to buy
the cheapest, positives = vendor 2 is cheapest, negitaves, obviosly
vendor #1 is the cheapest
From: Erik B. [mailto:[email protected]]
why don’t you just compare their averages?
kind regards -botp
Peña, Botp wrote:
From: Erik B. [mailto:[email protected]]
1.5 and -1.0 thing. It would be neat if it could just print *buy from
vendor #1" or “buy from vendor #2” *saying that you always
want to buy the cheapest, positives = vendor 2 is cheapest, negitaves,
obviosly vendor #1 is the cheapest*
why don’t you just compare their averages?
kind regards -botp
you mean like the avarage of all the items together from each vendor, if
so… Well im not sure how much you know about the food industry, but,
overtime if you dont find the cheapest prices, it adds up quick,
epecially when your running a big catering company like my dad :P. But i
was thinking… couldn’t i put if and elsifs for it to work???
On 7/26/07, Erik B. [email protected] wrote:
you mean like the avarage of all the items together from each vendor, if
so… Well im not sure how much you know about the food industry, but,
overtime if you dont find the cheapest prices, it adds up quick,
epecially when your running a big catering company like my dad :P. But i
was thinking… couldn’t i put if and elsifs for it to work???
Also look at case-when and <=>
For example,
v1, v2 = [10, 9, 12], [8, 9, 13]
best = a.zip(b).map do |pair|
case pair[0] <=> pair[1]
when -1 then ‘vendor a’
when 0 then ‘same’
when 1 then ‘vendor b’
end
end
Eventually, you might want to use Hashes…
v1 = { :onions => 0.70, :carrots => 0.88 }
v2 = { :onions => 0.78, :carrots => 0.87, :celery => 0.61 }
And other cool stuff like that
Todd
On Behalf Of Erik B.:
ah, i was imagining things am sorry, i thought each row stands for
one item only; meaning, an item can have many prices (thru history) from
a vendor. i assume vendors has many products and vice versa, and
products can have many prices. In your case, i think you only get the
latest single price on each vendor, so something like
Product1
Vendor1 -> Price
Vendor2 -> Price
Product2
Vendor1 ->Price
Vendor2 ->Price
Vendor3 ->Price
…
this is easy on database, but let’s use a hash just to illustrate,
C:\family\ruby>cat -n test.rb
1 # assume our dbpricing hash is our database
2 dbpricing =
3 {
4 “product1” =>
5 {“vendor1” => 110,
6 “vendor2” => 100,
7 “vendor3” => 105,
8 },
9 “product2” =>
10 {“vendor1” => 115,
11 “vendor2” => 110,
12 “vendor3” => 105,
13 “vendor4” => 105,
14 },
15 “product3” =>
16 {“vendor1” => 90,
17 “vendor2” => 110,
18 },
19 }
20
21 # let’s print our small db
22 # ppv stands for pricing per vendor
23 # for each product, we print our favored price and vendor
24 dbpricing.each do |product, ppv|
25 puts “Product: #{product}”
26 least_price = 1.0/0
27 least_price_vendor = “”
28 ppv.each do |vendor,price|
29 puts " #{vendor}: #{price}"
30 if price < least_price
31 least_price = price
32 least_price_vendor = vendor
33 end
34 end
35 puts " --------------"
36 puts " Winner: #{least_price_vendor} w #{least_price}"
37 end
now let’s test
Winner: vendor1 w 90
note, that we did not cater for ties and the vendors are not sort, so
those are left as an exercise
kind regards -botp
hummmmm, well it just kinda of needs to be clean like
vendor1-vendro1-vendor2
for prices… also where do you enter things in the db?
Erik B. wrote:
hummmmm, well it just kinda of needs to be clean like
vendor1-vendro1-vendor2
for prices… also where do you enter things in the db?
Sorry, i was a bit short on my last quote, I had leave for work and
didn’t really read all of your post.
The hash is nice, but it takes too long to enter each individual price,
the vendors update prices every day, so entering individual prices 3
times for each product takes longer than just highlighting like normal.
Although, im not sure how databases work… maybe its faster than hashes.
You idea is pretty good, I would ajust it a bit to make it easier to
read. Instead of listing all the prices and things just simply put for
example strawberries: vendor#1. What if you saped the vendor and the
pricing in you hash? that way you could just copy ans paste the prices
into it, as long as the prices are in the correct order.
On Behalf Of Erik B.:
if you have data. a database is the way to go. the fundamental idea is
that data and code should be separate. data should not be seen by the
programmer, only knowledge of its structure. Your program in turn, will
be structured too. You’ll have to create a data entry program separate
from your report program. The data entry program will be in charged of
putting the data into the database. But anyway, in your case, just
relax, go slowly (since you’ll be learning ruby also). a database is
another beast to tame and you’ll be learning new things too specially on
normalization and indexes (there are many databases, you’ll have to
scout it yourself, a quick list are: pstore, dbm/berkelydb, kirbybase,
sqlite, mysql, postgresql, etc… ). i’ll show you another scheme using
yaml… see below…
maybe you can show us how you want the data structured/formatted. do not
worry, as long as you can read it, it is structured.
as said, i’m going to show you another style. The data will be
structured/stored by vendor, then by product, this assumes like “I am
vendor#x, and this is my a price list of all products i’m selling”. But
the report will rotate it so that you get product pricing w
corresponding vendors, wc assumes like “I am the owner. I need a list of
my products w corresponding pricing sorted for each vendor provider”.
In this example, the data is stored separate on a yaml text file (just
read on yaml, it’s highly documented).
vendor3:
product1: 90
product2: 110
vendor1:
product1: 110
product2: 100
product3: 105
vendor2:
product1: 115
product2: 110
product3: 105
product4: 105
C:\family\ruby>cat test2.rb
require ‘yaml’
dbpricing = YAML.load(File.open(“dbpricing.yaml”))
puts “------------------”
puts “Vendors Price List”
puts “------------------”
dbpricing.each do |vendor, ppp|
puts “Vendor: #{vendor}”
ppp.each do |product,price|
puts " #{product}: #{price}"
end
end
dbpricing2 = Hash.new{|h,k| h[k]={} }
dbpricing.each do |vendor, ppp|
ppp.each do |product,price|
dbpricing2[product][vendor] = price
end
end
puts
puts “--------------”
puts “Our Price List”
puts “--------------”
dbpricing2.each do |product, ppv|
puts “Product: #{product}”
ppv.sort{|v1,v2| v1[1] <=> v2[1] }.each do |pair|
puts " #{pair[0]}: #{pair[1].to_s.rjust(4)}"
end
end
Vendor: vendor3
product1: 90
product2: 110
Vendor: vendor1
product1: 110
product2: 100
product3: 105
Vendor: vendor2
product1: 115
product2: 110
product3: 105
product4: 105
Product: product1
vendor3: 90
vendor1: 110
vendor2: 115
Product: product2
vendor1: 100
vendor3: 110
vendor2: 110
Product: product3
vendor1: 105
vendor2: 105
Product: product4
vendor2: 105
C:\family\ruby>
hope that helps.
kind regards -botp
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs