"FISH" will always be in "SHELLFISH" but what if a string has both FISH and SHELLFISH in it? - Nicelabel 5.0

Viewed 82

I am very new to programming and I am writing some code to work with Nicelabel 5.0 to display small pictures of allergens on a product label when the allergen is present within a string. I thought I really nailed it by using inStr to see if certain strings were present. I was even able to make the program distinguish between "FISH" and "SHELLFISH"

if inStr (1, Allergens1, "FISH") > 0 then
         if inStr (1, Allergens1, "SHELLFISH") > 0 then
         a = label.SetObjectVisible("SHELLFISH", true)
         else
         a = label.SetObjectVisible("FISH", true)
         end if
end if

Where I'm stuck is what do I do if a product has BOTH shellfish and fish in it? The string "FISH" will always appear in the string "SHELLFISH" so how can I distinguish between the two?

Please let me know if more information is needed. The variable allergens1 is just a simple string ex.

"CONTAINS: EGG, FISH(SALMON), SHELLFISH(OYSTERS)"

I need to work within the confines of the labeling software which actually uses VBScript. I'm sorry for any confusion.

THANK YOU TO EVERYONE WHO HELPED WITH THIS! The final working code is below and should be noted will work with older version of Nicelabel that still use VBScript for label functions:

''' 
Private Function GetWords(sText)
GetWords = 
Split(Trim(Replace(Replace(Replace(Replace(Replace(Replace(sText, ":", " "), ",", " "), "(", " "), ")", " "), "  ", " "), "  ", " ")))
End Function
Dim vElem
dim a
  a = label.SetObjectVisible("TREENUT",false)
  a = label.SetObjectVisible("EGG",false)
  a = label.SetObjectVisible("SOY", false)
  a = label.SetObjectVisible("WHEAT", false)
  a = label.SetObjectVisible("NUT", false)
  a = label.SetObjectVisible("SHELLFISH", false)
  a = label.SetObjectVisible("FISH", false)
  a = label.SetObjectVisible("MILK", false)
  a = label.SetObjectVisible("SESAME", false)
  a = label.SetObjectVisible("SULFITE", false)
For Each vElem In GetWords(Allergens1)
a = label.SetObjectVisible(vElem,True)
Next

'''

4 Answers

I'm pretty new myself so grain of salt and whatnot, but I think that first I'd split the string into several strings using the comma as a separator and making it a list of ingredients that you can then use your if statement on

A simple substring search over the whole string will not suffice, as you have discovered. You need to "tokenize" your string, ie split the string:

"EGG, FISH(SALMON), SHELLFISH(OYSTERS)"

into separate substrings:

"EGG"
"FISH"
"SALMON"
"SHELLFISH"
"OYSTERS"

And then you can iterate and compare each substring as-is as needed.

The approach I would take, as mentioned by several people, is to tokenize your string and then have your display logic use that list of tokens. Something like the following:

Dim Description
Dim Allergens
   
Description = "CONTAINS: EGG, FISH(SALMON)"
WScript.Echo Description
Set Allergens = GetAllergens(Description)
DisplayAllergens Allergens
WScript.Echo ""
   
Description = "CONTAINS: EGG, SHELLFISH(OYSTERS)"
WScript.Echo Description
Set Allergens = GetAllergens(Description)
DisplayAllergens Allergens
WScript.Echo ""
   
Description = "CONTAINS: EGG, FISH(SALMON), SHELLFISH(OYSTERS)"
WScript.Echo Description
Set Allergens = GetAllergens(Description)
DisplayAllergens Allergens
WScript.Echo ""

Private Function GetAllergens(ByVal Description)
   Dim Allergens
   Dim Allergen
   Dim i
   
   Set GetAllergens = CreateObject("Scripting.Dictionary")
   Description = Replace(Description, "CONTAINS: ", "")
   Allergens = Split(Description, ",")
   
   For i = LBound(Allergens) To UBound(Allergens)
      Allergen = Trim(Split(Allergens(i), "(")(0))
      GetAllergens.Add Allergen, Allergen
   Next
End Function

Private Sub DisplayAllergens(ByVal Allergens)
   Dim Allergen
   
   For Each Allergen In Allergens
      If Allergen = "EGG" Then WScript.Echo "EGG picture"
      If Allergen = "SHELLFISH" Then WScript.Echo "SHELLFISH picture"
      If Allergen = "FISH" Then WScript.Echo "FISH picture"
   Next
End Sub

Here is the result:

enter image description here

Here is a one-liner which splits your string into words:

Private Function GetWords(sText)
    GetWords = Split(Trim(Replace(Replace(Replace(Replace(Replace(Replace(sText, ":", " "), ",", " "), "(", " "), ")", " "), "  ", " "), "  ", " ")))
End Function

You can use this with For Each like this:

Dim Allergens1
Dim vElem

Allergens1 = "CONTAINS: EGG, FISH(SALMON), SHELLFISH(OYSTERS)"
For Each vElem In GetWords(Allergens1)
    WScript.echo vElem & " picture"
Next

. . . to get this:

CONTAINS picture
EGG picture
FISH picture
SALMON picture
SHELLFISH picture
OYSTERS picture
Related