Search code examples
lua

Searching for strings{patterns?) that have white space or repeated characters in string LUA


I am trying to search a string that contains other strings with spaces and/or repeated characters. Below is a snippet of the code.

It works fine with things like AJS37 or F-14A but fails (the serach falls thgough to the last if) on Bf-109K-4 or Ka-50 III.

I am very much new to lua, and pretty new to coding overall.

Thanks in advance for any and all help.

Nathan C.

local Interceptor_Types = "AJS37, Bf-109K-4, F-14A, F-14B, F-15C, F-16C bl.50, F-5E-3, F-86F Sabre, FA-18C_hornet, FW-190A8, FW-190D9, J-11A, JF-17, M-2000C, MiG-15bis, MiG-19P" 
                          .. ", MiG-21Bis, MiG-29A, MiG-29G, MiG-29S, Mirage-F1CE, Mirage-F1EE, MosquitoFBMkVI, P-47D-30, P-47D-30 (Early) , P-47D-40, P-51D-25-NA, P-51D-30-NA"  
                          .. ", SpitfireLFMkIX, SpitfireLFMkIXCW, Su-27, Su-33"
local CAS_Types         = "Start, A-4E-C, AV8BNA, C-101CC, L-39C, L-39ZA, Yak-52, A-10A, A-10C, A-10C_2, C-101EB, Christen Eagle II, I-16, Su-25, Su-25T, TF-45, TF-51D"
local Helicopter_Types  = "AH-64D_BLK_II, Ka-50, Ka-50 III, Mi-24P, Mi-8MT, SA342L, SA342M, SA342Minigun, SA342Mistral, UH-1H, UH-1M Short Body, AH-6J, Bell 47, CH-47 Chinook"  
                          .. ", CH-53E, CH-53E Super Stallion,UH-60L Blackhawk" 
                          
local Num_Interceptors = 0
local Num_CAS = 0
local Num_Helicopters = 0
local Num_Others = 0
local aName = "CH-47%sChinook" 
print ("The search string is " .. aName)
if string.find (Interceptor_Types, aName) then
    Num_Interceptors = Num_Interceptors +1
    print("TEST Output 3: " ..  Num_Interceptors)     -- Outputs number of clients
    elseif string.find (CAS_Types, aName) then
    Num_CAS = Num_CAS +1
    print("TEST Output 4: " ..  Num_CAS)     -- Outputs number of clients
    elseif  string.find (Helicopter_Types, aName) then
    Num_Helicopters = Num_Helicopters +1
    print("TEST Output 5: " ..  Num_Helicopters)     -- Outputs number of clients
    else
    Num_Others = Num_Others + 1
end 
print ("The Interceptor types are: " .. Interceptor_Types)
print ("The CAS types are: " .. CAS_Types)
print ("The Helicopter types are: " .. Helicopter_Types)

print("Num_Interceptors Outside of if statement: " ..  Num_Interceptors)     -- Outputs number of clients
print("Num_CAS Outside of if statement: " ..  Num_CAS)     -- Outputs number of clients
print("Num_Helicopters Outside of if statement: " ..  Num_Helicopters)     -- Outputs number of clients
print("Num_Others Outside of if statement: " ..  Num_Others)     -- Outputs number of clients

I did try several searches, some worked some didn't. I did try"Ka-50%sIII" but that also fell through to the last catch all "else"


Solution

  • Your "catch all else" seems to be missing an end. That said, one obvious issue I see with your code is that you're not escaping magic characters in your search pattern properly: The pattern Ka-50%sIII does not work like you think it does. - is a quantifier in Lua meaning "zero or more of the preceding character(set), and as few as possible". This would match K50 III or Kaaaaaa50 III, but it won't match a literal minus. To match a literal minus, simply escape the magic character using a percent sign: Ka%-50%sIII matches the literal string Ka-50 followed by any space character and III.

    I suggest you to take a look at the Lua 5.4 reference manual section on patterns.