none
Require help with simlpe text commands RRS feed

  • Question

  • Hello.

    I'm fairly new to Small Basic. I have written a simple program, to allow me to store contacts, and then search for them when typing in the name. The issue I have, is I have to specifically type in the exact name to find the contact. Is it possible to have abbreviations, or at least, have a list of more than 1 name for the same contact, so if I type in all the different names I've listed, it will find the relevant contact.... rather than creating a whole new contact just for the shorthand version of the name.

    And example of what my code looks like is this:

    friends["Legal And General"]["Name"] = "Legal And General"

    So in this situation, I have to type the whole name. I want to be able to type L&G or LandG or other alternatvies, to find the same contact.

    Any help would be greatly appreciated! Thanks! :)

    Wednesday, July 21, 2010 10:46 AM

Answers

  • If we go with your "Is it possible to have abbreviations, or at least, have a list of more than 1 name for the same contact", that would be easy.
    Just add anoter variable, like:

    friends["Legal And General"]["Name"] = "Legal And General"
    friends["Legal And General"]["NameAbreviation"] = "L&G"

    Otherwise, you will need to use methods in the "Text." object. to break down the search crieria, identifying what you are really trying to search for, and then break down the Name field/variable to match the parts.
    http://smallbasic.com/smallbasic.com/doc/?id=17&language=

    Hmmm...  I could have sworn there was a "Left", "Mid", and "Right" method, but the information on the link I provided doesn't show those.  Maybe something has changed, or am I thinking of Excel, Access, or some other programming language?

    Anyway, to search for a shorthand version of something is a little tricky.  Computers are literal and you have to use literal logic to tell the computer how to (seemingly) be not so literal.

    You would have to tell the program to look for "&" and/or "and" in your Search variable, but what if the Name contained "&" or "and" - like the word "Land" containes the subtext "and".  Your example above has the word "And" in the Name.
    So you would need to have some kind of unique way to identify your "and" in the search as being a boolean type command instead of subtext of your search - like making it all upper-case: "AND".
    You can also stick with using just "&" and tell the program to ignore any "&" in the Name field.

    If you still can't figure it out, paste all your code and I'll give you some more specific instructions.

    THNX!

    Wednesday, July 21, 2010 2:17 PM
  • You can use the text options to look for sub-strings, how about something like the following:

    PS you can use the publish option to exchange files - very long files without specific problems are hard to help with though - if you can create a short example that shows any problem you are working on.

    friends["Legal And General"]["Name"] = "Legal And General"
    friends["Legal Aid"]["Name"] = "Legal Aid"
    friends["Jim McDonald"]["Name"] = "Jim McDonald"
    friends["Jimmy Smith"]["Name"] = "Jimmy Smith"

    While ("True")
      TextWindow.ForegroundColor = "Yellow"
      TextWindow.Write("Enter search text: ")
      TextWindow.ForegroundColor = "White"
      search = TextWindow.Read()
      searchFriends()
      If (found <> "") Then
        TextWindow.ForegroundColor = "Red"
        TextWindow.Write(found["Name"])
        TextWindow.ForegroundColor = "White"
        TextWindow.WriteLine(" Selected")
      EndIf
    EndWhile

    Sub searchFriends
      elemets = Array.GetAllIndices(friends) 'Array of indexes present in array friends
      found = "" 'empty array of found matches
      ifound = 0
      For i = 1 To Array.GetItemCount(elemets)
        friend = friends[elemets[i]] 'Array of elements for the i'th entry in friends
        name = Text.ConvertToLowerCase(friend["Name"]) 'The name of the current element (converted to lower case to ease text comparisons)
        searchLC = Text.ConvertToLowerCase(search) 'Lower case for the search string
        If (Text.IsSubText(name,searchLC)) Then 'Subtext found
          ifound = ifound + 1
          found[ifound] = friend
          TextWindow.ForegroundColor = "White"
          TextWindow.Write(ifound+" : ")
          TextWindow.ForegroundColor = "Green"
          TextWindow.Write(friend["Name"])
          TextWindow.ForegroundColor = "White"
          TextWindow.WriteLine(" Found")
        EndIf
      EndFor
      If (found = "") Then 'No match
        TextWindow.ForegroundColor = "Red"
        TextWindow.Write(search)
        TextWindow.ForegroundColor = "White"
        TextWindow.WriteLine(" Not found")
      Else 'Select one of the matches
        If (Array.GetItemCount(found) = 1) Then 'Only one match -use it
          found = found[1]  
        Else 'Select one of the matches
          TryAgain:
          TextWindow.ForegroundColor = "Yellow"
          TextWindow.Write("Select the friend you want: ")
          TextWindow.ForegroundColor = "White"
          ifound = TextWindow.ReadNumber()
          If (ifound < 1 Or ifound > Array.GetItemCount(found) Or ifound <> Math.Floor(ifound)) Then 'Check for valid entry (integer in range)
            Goto TryAgain
          EndIf
          found = found[ifound]
        EndIf
      EndIf
    EndSub

    Wednesday, July 21, 2010 7:27 PM
    Moderator

All replies

  • If we go with your "Is it possible to have abbreviations, or at least, have a list of more than 1 name for the same contact", that would be easy.
    Just add anoter variable, like:

    friends["Legal And General"]["Name"] = "Legal And General"
    friends["Legal And General"]["NameAbreviation"] = "L&G"

    Otherwise, you will need to use methods in the "Text." object. to break down the search crieria, identifying what you are really trying to search for, and then break down the Name field/variable to match the parts.
    http://smallbasic.com/smallbasic.com/doc/?id=17&language=

    Hmmm...  I could have sworn there was a "Left", "Mid", and "Right" method, but the information on the link I provided doesn't show those.  Maybe something has changed, or am I thinking of Excel, Access, or some other programming language?

    Anyway, to search for a shorthand version of something is a little tricky.  Computers are literal and you have to use literal logic to tell the computer how to (seemingly) be not so literal.

    You would have to tell the program to look for "&" and/or "and" in your Search variable, but what if the Name contained "&" or "and" - like the word "Land" containes the subtext "and".  Your example above has the word "And" in the Name.
    So you would need to have some kind of unique way to identify your "and" in the search as being a boolean type command instead of subtext of your search - like making it all upper-case: "AND".
    You can also stick with using just "&" and tell the program to ignore any "&" in the Name field.

    If you still can't figure it out, paste all your code and I'll give you some more specific instructions.

    THNX!

    Wednesday, July 21, 2010 2:17 PM
  • Thanks, very decent of you to explain it all.

    I understand the most part... but I think my code is getting more and more complicated as time goes on... and the more I try to resolve something, the worse it gets! Hahaha.

    Mind if I send you my code, and maybe you can give me some suggestions to actually get it functioning correctly? What's the best idea, post it in here, or email it to you? It's fairly long.

    Wednesday, July 21, 2010 2:24 PM
  • You can use the text options to look for sub-strings, how about something like the following:

    PS you can use the publish option to exchange files - very long files without specific problems are hard to help with though - if you can create a short example that shows any problem you are working on.

    friends["Legal And General"]["Name"] = "Legal And General"
    friends["Legal Aid"]["Name"] = "Legal Aid"
    friends["Jim McDonald"]["Name"] = "Jim McDonald"
    friends["Jimmy Smith"]["Name"] = "Jimmy Smith"

    While ("True")
      TextWindow.ForegroundColor = "Yellow"
      TextWindow.Write("Enter search text: ")
      TextWindow.ForegroundColor = "White"
      search = TextWindow.Read()
      searchFriends()
      If (found <> "") Then
        TextWindow.ForegroundColor = "Red"
        TextWindow.Write(found["Name"])
        TextWindow.ForegroundColor = "White"
        TextWindow.WriteLine(" Selected")
      EndIf
    EndWhile

    Sub searchFriends
      elemets = Array.GetAllIndices(friends) 'Array of indexes present in array friends
      found = "" 'empty array of found matches
      ifound = 0
      For i = 1 To Array.GetItemCount(elemets)
        friend = friends[elemets[i]] 'Array of elements for the i'th entry in friends
        name = Text.ConvertToLowerCase(friend["Name"]) 'The name of the current element (converted to lower case to ease text comparisons)
        searchLC = Text.ConvertToLowerCase(search) 'Lower case for the search string
        If (Text.IsSubText(name,searchLC)) Then 'Subtext found
          ifound = ifound + 1
          found[ifound] = friend
          TextWindow.ForegroundColor = "White"
          TextWindow.Write(ifound+" : ")
          TextWindow.ForegroundColor = "Green"
          TextWindow.Write(friend["Name"])
          TextWindow.ForegroundColor = "White"
          TextWindow.WriteLine(" Found")
        EndIf
      EndFor
      If (found = "") Then 'No match
        TextWindow.ForegroundColor = "Red"
        TextWindow.Write(search)
        TextWindow.ForegroundColor = "White"
        TextWindow.WriteLine(" Not found")
      Else 'Select one of the matches
        If (Array.GetItemCount(found) = 1) Then 'Only one match -use it
          found = found[1]  
        Else 'Select one of the matches
          TryAgain:
          TextWindow.ForegroundColor = "Yellow"
          TextWindow.Write("Select the friend you want: ")
          TextWindow.ForegroundColor = "White"
          ifound = TextWindow.ReadNumber()
          If (ifound < 1 Or ifound > Array.GetItemCount(found) Or ifound <> Math.Floor(ifound)) Then 'Check for valid entry (integer in range)
            Goto TryAgain
          EndIf
          found = found[ifound]
        EndIf
      EndIf
    EndSub

    Wednesday, July 21, 2010 7:27 PM
    Moderator
  • Thanks Litdev.

    I'll go ahead and publish some of the code... as to be honest, the code is just repeated, over and over.

    If you have any advice on my other query - http://social.msdn.microsoft.com/Forums/en-US/smallbasic/thread/1f2d9b26-f2e5-4508-aa95-2b07fdb14439 - that will likely resolve this query too. (I plan on having 2 programs with the 'friends' code, but holindg different types of information. I wanted a graphics window to load, with 2 buttons, allowing you to select which program to launch.)

    Again, thanks a lot in advance! :)

    Thursday, July 22, 2010 7:49 AM
  • Okay here is an example of the code.

    friends["Legal and General"]["Name"] = "Legal and General - Ben / Jym"
    friends["Legal and General"]["Phone"] = "0845 270 7010"
    friends["Legal and General"]["Website"] = "www.legalandgeneral.com"
    friends["Legal and General"]["Email"] = "None"

    So if you type Legal and General - it will find all the information for that provider. But some people may try and search for them as L&G and it obviously won't find anything. So I needed a way for me to type in as many abbreviations as possible, so all searchs will find the correct provider. The only way I know of is to write out the entire code again for each abbreviation I can think of.... which is most definitely not a good idea as the code would become massive and almost impossible to maintain.

     

    And just to add, I really liked the code Litdev. I was playing around with it, and is there a way so when I search Legal And General, it shows that it found it, but it also lists the extra info, such as telephone number, website etc?

    Thursday, July 22, 2010 9:29 AM
  • The fact that you are programming this, instead of using any number of programs that can easily keep and search contact information, makes me think you are a do-it-yourself -er.
    So I am guessing you would rather be tutored along instead of someone writing out the code for you.

    Click the "Publish" button, on the Small Basic toolbar, and then post the number.
    I'll go over your code and point you in the right direction, even giving you code to use.
    Looking at your code gives me a good idea of the direction YOU want to go instead of the way I think you should go. (Nothing against litdev's help - all the help he has given in the Small Basic forum is awesome!  I'm just explaining my approach, that's all.)

    I don't learn that much when someone does something for me and I would prefer help rather than someone do it for me but if you, for what ever reason, would rather it be typed out for you, then let me know that.  :-)

    Thursday, July 22, 2010 1:31 PM
  • Thanks Path_drc.

    The program is for work, as we have all the information, but takes us years to find it - so I took it upon myself to make a simple program that we can use to grab info when we need it.

    Learning the code would be great, or even talking me through the code as you write it. I'll go ahead andpublish it (if it stops popping up with errors).

    Thanks :)

    Thursday, July 22, 2010 3:34 PM
  • Scottie,

     

    This is probably a bit too complex for you to dig through, but it may help... I wrote a search program as an example to look for music track titles and artists using wildcards '*'.  You can import it using code: SGL178

     

    As Path_DRC suggested, to allow multiple names for each contact, just add friends["Legal And General"]["NameAbreviation"] = "L&G" or an Alias or field.

     

    Personally, I would change your format so that the first array element is numerical, which will make it slightly easier to work with, so...

     

    friends[1]["Company"]="Legal & General"

    friends[1]["Contact"]="Bob Smith"

    friends[1]["Keywords"]="L&G, Legal and General, Robert Smith, Rob Smith, " + friends[1]["Contact"] + ", " + friends[1]["Company"]

     

    friends[2]["Company"]="Direct Line"

    friends[2]["Contact"]="Jane Jones"

    friends[2]["Keywords"]="red telephone, insurance, car, home, " + friends[2]["Contact"] + ", " + friends[2]["Company"]


     

    searchFor="Bob"

    for lwp=1 to 2

    If text.GetIndexOf( friends[lwp]["Keywords"], searchFor ) > 0 Then

    textwindow.writeline( friends[lwp]["Company"] )

    textwindow.writeline( friends[lwp]["Contact"] )

    EndIf

    EndFor


    Thursday, July 22, 2010 10:40 PM
  • Ah failed to import. Must be the network here at work I presume.
    Tuesday, July 27, 2010 2:29 PM