Joshua Drake <jd.nospam / commandprompt.com> writes:

> #!/usr/bin/ruby
> begin
>  myfile = open('phonespec.txt', 'a')
>  $closing = "\n\nTo start the program again please type intro2.rb\n\tGood 
> Bye!\n\n"
> 
>     enterFirstName = ""    #First Name in the addressbook
>     enterLastName = ""    #Last Name in the Addressbook
>     enterPhoneNumber = "" #Phone Number in the addressbook
> 
>   print "Welcome to MyAddressbook. Please follow the prompts.\n"
>   print "If you wish to end data entry, you may do so at any time\n"
>   print "by type the word END into a prompt.\n\n"
>  while 1
>    print "\n\tFirst Name: "
>    enterFirstName = STDIN.gets
>    enterFirstName.chop!
>    if enterFirstName == "END"
>      print $closing
>      break
>    end
>    if enterFirstName != "END"
>      print "\n\tLast Name: "
>      enterLastName = STDIN.gets
>      enterLastName.chop!
>   end
>   if enterLastName == "END"
>     print $closing
>     break
>   end
>   if enterLastName != "END"
>     print "\n\tPhone Number: "
>     enterPhoneNumber = STDIN.gets
>     enterPhoneNumber.chop!
>   end
>   if enterPhoneNumber == "END"
>     print $closing
>     break
>   end
>   myfile.write(enterFirstName+"\t"+enterLastName+"\t"+enterPhoneNumber+"\n")
>  end
>    ensure
>      myfile.close
> end

How about something like this:


     def prompt_and_read(prompt)
       print "\n", prompt, ": "
       result = gets
       throw :done if !result || result == "END\n"
       result.chop
     end


     File.open('phonespec.txt', 'a') do |myfile|

       puts "Welcome to MyAddressbook. Please follow the prompts."
       puts "If you wish to end data entry, you may do so at any time"
       puts "by type the word END into a prompt."

       catch (:done) do 
         loop do
           first_name = prompt_and_read "First name"
           last_name  = prompt_and_read "Last name"
           phone      = prompt_and_read "Phone"

           myfile.puts first_name + "\t" + last_name + "\t" + phone
         end
       end
     end

     puts "\n\nTo start the program again please type intro2.rb"
     puts "\tGood Bye!\n\n"

or, perhaps tidier

     FIELDS = [ "First name", "Last name", "Phone" ]

     File.open('phonespec.txt', 'a') do |myfile|

       puts "Welcome to MyAddressbook. Please follow the prompts."
       puts "If you wish to end data entry, you may do so at any time"
       puts "by type the word END into a prompt."

       catch (:done) do
         loop do
           result = []
           FIELDS.each do |prompt|
             print prompt, ": "
             answer = gets
             throw :done if !answer || answer == "END\n"
             result << answer.chop
           end
           myfile.puts result.join("\t")
         end
       end
     end

     puts "\n\nTo start the program again please type intro2.rb"
     puts "\tGood Bye!\n\n"

> == end script 1
> 
> == begin script 2
> phoneData = IO.readlines("phonespec.txt");      # Read in file into array, 
> default separator of '\n'
> 
> print "\n\nYou are searching for:\t"
> searchData = STDIN.gets
> searchData.chop!
> myreg = Regexp.new(searchData, "i")     # The "i" option makes it 
> case-insensitive.
> numres = 0                              # Init number of results at 0.
> 
> for data in phoneData                   # For each line of data in our array...
>   if myreg =~ data                      # Compare our regex against this line.
>     print "Found result: " + data       # Output line that matched the regex.
>     numres += 1                         # Increment number of results found.
>   else
>   end
> end
> 
> print "\n\n(Found " + String(numres) + " results, while searching for '" + 
> searchData + "' in 'phonespec.txt')\n\n"
> == end script 2

Perhaps (this takes the search string from the command line)

     res = File.open("phonespec.txt").grep /#{ARGV[0]}/i
     puts res, "Found #{res.length} results"

If you don't need to report the count, then:

     puts File.open("phonespec.txt").grep /#{ARGV[0]}/i



Regards


Dave