# String Matching in an Array

**URL:** <https://www.ruby-forum.com/t/string-matching-in-an-array/99022>\
**Category:** Ruby\
**Created:** [July 19, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/string-matching-in-an-array/99022 "2006-07-19T20:57:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![diragor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6a8cbe/32.png) [@diragor](https://www.ruby-forum.com/u/diragor)\
**Post date:** [July 19, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/string-matching-in-an-array/99022/1 "2006-07-19T20:57:44Z")

</div>

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[i] if lookup ==

@purchase\_orders[i].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[i] == nil then break  
end  
if find\_po.to\_s == aPOlist[i].po\_number  
found\_one = true  
puts aPOlist[i]  
end  
end  
if found\_one == false  
puts “PO NOT FOUND”  
menu\_screen.root  
end

---

<div class="post-metadata">

**Author:** ![diragor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6a8cbe/32.png) [@diragor](https://www.ruby-forum.com/u/diragor)\
**Post date:** [July 19, 2006, 9:00pm UTC](https://www.ruby-forum.com/t/string-matching-in-an-array/99022/2 "2006-07-19T21:00:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![diragor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6a8cbe/32.png) [@diragor](https://www.ruby-forum.com/u/diragor)\
**Post date:** [July 19, 2006, 9:16pm UTC](https://www.ruby-forum.com/t/string-matching-in-an-array/99022/3 "2006-07-19T21:16:35Z")

</div>

Ryan Edwards-crewe wrote:

> 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

---

<div class="post-metadata">

**Author:** ![diragor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6a8cbe/32.png) [@diragor](https://www.ruby-forum.com/u/diragor)\
**Post date:** [July 19, 2006, 10:04pm UTC](https://www.ruby-forum.com/t/string-matching-in-an-array/99022/4 "2006-07-19T22:04:19Z")

</div>

Most excellent thank you, it’s working now, it’s always something small  
😛  
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.

---

<div class="post-metadata">

**Author:** ![diragor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6a8cbe/32.png) [@diragor](https://www.ruby-forum.com/u/diragor)\
**Post date:** [July 19, 2006, 10:36pm UTC](https://www.ruby-forum.com/t/string-matching-in-an-array/99022/5 "2006-07-19T22:36:52Z")

</div>

system ‘clear’

Just shell out to the way it is usually done.

mark

---

<div class="post-metadata">

**Author:** ![diragor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6a8cbe/32.png) [@diragor](https://www.ruby-forum.com/u/diragor)\
**Post date:** [July 19, 2006, 10:57pm UTC](https://www.ruby-forum.com/t/string-matching-in-an-array/99022/6 "2006-07-19T22:57:44Z")

</div>

Thanks Mark and Chris duely noted.

---

<div class="post-metadata">

**Author:** ![diragor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6a8cbe/32.png) [@diragor](https://www.ruby-forum.com/u/diragor)\
**Post date:** [July 19, 2006, 10:33pm UTC](https://www.ruby-forum.com/t/string-matching-in-an-array/99022/7 "2006-07-19T22:33:43Z")

</div>

Ryan Edwards-crewe wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).  
> 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
