String Matching in an Array

R

Ryan Edwards-crewe

I have an array set up that I want to match the user input to a property
of a sting. However whenever I try to impliment the code the stings
never match and I am returned nil.

The first two ways I tried:
class POlist
def find(lookup)
@purchase_orders.find { |aPOlist| lookup == aPOlist.po_number }
# for i in 0...@purchase_orders.length
# return @purchase_orders if lookup ==
@purchase_orders.po_number
# end
# return nil
end
end

and the other one:

found_one = false
print "Please Enter a PO: "
find_po = gets
for i in 0..aPOlist.length
if aPOlist == nil then break
end
if find_po.to_s == aPOlist.po_number
found_one = true
puts aPOlist
end
end
if found_one == false
puts "PO NOT FOUND"
menu_screen.root
end
 
R

Ryan Edwards-crewe

Forgot to mention I'm trying to match an 8 digit number, but I pretty
sure it's being stored as a string.

I am taking the values from an Excel worksheet

po_number = worksheet.Range("#{col}#{row}").value

if po_number.length == 8 #the standard Purchase Order length
aPOlist.append(PurchaseOrder.new("#{po_number}", "#{notes}",
"#{company}", "#{unit_config}", "#{due_date}", "#{quantity.to_i}"))
end
 
T

Tom Werner

Ryan said:
Forgot to mention I'm trying to match an 8 digit number, but I pretty
sure it's being stored as a string.

I am taking the values from an Excel worksheet

po_number = worksheet.Range("#{col}#{row}").value

if po_number.length == 8 #the standard Purchase Order length
aPOlist.append(PurchaseOrder.new("#{po_number}", "#{notes}",
"#{company}", "#{unit_config}", "#{due_date}", "#{quantity.to_i}"))
end
Don't forget to chomp the input to get rid of the newline:

irb(main):001:0> val = gets
12345678
=> "12345678\n"
irb(main):002:0> val = gets.chomp
12345678
=> "12345678"
irb(main):003:0>

Also, instead of doing

for i in 0..aPOlist.length
#code
end

a prettier way is

aPOlist.each_index do |i|
#code
end



--
Tom Werner
Helmets to Hardhats
Software Developer
(e-mail address removed)
www.helmetstohardhats.org
 
R

Ryan Edwards-crewe

Most excellent thank you, it's working now, it's always something small
:p
But if you don't mind me picking your brain, or anyone's for that
matter. Is there a way I can clear the screen of the console from ruby,
(eg. cls in terminal or windows command prompt), things just get real
messy looking when all it does is scroll.

Thanks.
 
C

ChrisH

Ryan said:
Most excellent thank you, it's working now, it's always something small
:p
But if you don't mind me picking your brain, or anyone's for that
matter. Is there a way I can clear the screen of the console from ruby,
(eg. cls in terminal or windows command prompt), things just get real
messy looking when all it does is scroll.

Thanks.

use the system command:
system('cls')
Or
`cls` #note the back-quotes

Note that Array (and anything that mixes in Enumerable) has a #find
method:

anPOlist.find {|aPO| aPO.poNumber == aPOnumber}

will return the first match, or nil if not found

cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top