String Matching in an Array

Discussion in 'Ruby' started by Ryan Edwards-crewe, Jul 19, 2006.

  1. 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

    --
    Posted via http://www.ruby-forum.com/.
    Ryan Edwards-crewe, Jul 19, 2006
    #1
    1. Advertising

  2. 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

    --
    Posted via http://www.ruby-forum.com/.
    Ryan Edwards-crewe, Jul 19, 2006
    #2
    1. Advertising

  3. Ryan Edwards-crewe

    Tom Werner Guest

    Ryan Edwards-crewe wrote:
    > 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

    www.helmetstohardhats.org
    Tom Werner, Jul 19, 2006
    #3
  4. 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.

    --
    Posted via http://www.ruby-forum.com/.
    Ryan Edwards-crewe, Jul 19, 2006
    #4
  5. Ryan Edwards-crewe

    ChrisH Guest

    Ryan Edwards-crewe wrote:
    > 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.
    >
    > --
    > Posted via 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
    ChrisH, Jul 19, 2006
    #5
  6. Ryan Edwards-crewe, Jul 19, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Psybar Phreak
    Replies:
    1
    Views:
    551
    Eric Sosman
    Oct 6, 2003
  2. =?Utf-8?B?UmFqZXNoIHNvbmk=?=

    'System.String[]' from its string representation 'String[] Array'

    =?Utf-8?B?UmFqZXNoIHNvbmk=?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,784
    =?Utf-8?B?UmFqZXNoIHNvbmk=?=
    May 4, 2006
  3. =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    Replies:
    5
    Views:
    1,273
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    May 6, 2006
  4. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    220
    Marc Bissonnette
    Jan 13, 2004
  5. Bobby Chamness
    Replies:
    2
    Views:
    212
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page