Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Reconstruction of String completely unsuccessful?

I highly don’t rely on anybody understanding the purpose or process of this function, so I’ll do my best to explain the piece I’d rather focus on. For those who have knowledge in String Patterns, this would probably be easy for you to grasp: I’m not sure if I’m just during stupid somewhere in the piece I’m having trouble with, or this truly is a problem, but what I’m trying to do here is cut off a piece from a String if it’s equivalent to anything In the array prohibited. I’m using a gmatch to iterate through the String’s words, I’m trying to accomplish stripping out something if it’s not entieraly just apart of the word; apart of another E.g. “Hello there”, then remove that chunk, it doesn’t seem to work, though if I had the prohibited word equal to the exact, then it would’ve been able to remove it perfectly fine. Yet it doesn’t function properly for both transactions now, what have I done wrong?

String = "Some sentence here I guess?"
local prohibited = {"I guess"} --// or “guess”
function rewrite(String)
   local reString = {}
   if (String:match("[%w%p]+")) then
      for a in String:gmatch("[^%s]+") do
         for _,b in next, prohibited do --// function begins here
            if (a:find(b) or String:find(b)) then
               a = String:gsub(b, "") or a:gsub(b, "")
               break
            end
         end
         table.insert(reString, a); --// ends here
      end
   else return "empty"
   end
   return table.concat(reString, " ")
end
local rewritedstring = rewrite(String) 
if (rewritedstring ~= "empty") then
   print("String: '"..rewritedstring.."' - ('"..String.."')")
else return end

--// function is supposed to return the String cleaned up with the prohibited word(s) removed, the String is return normal without the word(s) removed at all.

again, this is going to be incredibly hard to understand if you don’t have a good knowledge of String Patterns, or I’m dumb and there’s something obvious, gl?;)

Extraction of a single word is functional now

2 comments

0

what is your expected output from the example given above?

0

It literally says it.. but for closure, it should look like so: ‘Some sentence here ?’ -> ‘I guess’ removed

2 answers

✓ Accepted answer

Right, so correct me if I am not on point, but it looks like you are just trying to remove any exact occurrences of the values in the prohibited table from String.

If that is the case, then you do not need to use any cool string patterns or iterate through each word with gmatch, you simply need to gsub each table entry in the string with an empty string.

Here is the code I ended up using.

local String = "Some sentence here I guess?"
local prohibited = {"I guess"}

local newString

for _, text in next, prohibited do
	newString = String:gsub(text, "")
end

print(String) --> Some sentence here I guess?
print(newString) --> Some sentence here ?

If I missed something or there was some other result you desired, it was not clear, so please reiterate in comments if that is the case.

Also, If you have any questions, let me know and I will edit my answer.

-amanda

There seems to be a bit of unnecessary code in your snippet. Firstly, I wouldn't use a nested for. The same goal can be accomplished with only one numeric for loop.

####For example...

local blacklist = {"these", "are not"}

local function filterStringWithList(str, list)
	for i = 1, #list do
		str = string.gsub(str, list[i], "")
	end
	return string.gsub(str, "%s+", " ")
end

local filtered = filterStringWithList(
	"these words are not allowed", 
	blacklist
)

print(filtered)

words allowed

If I'm understanding your problem correctly, I believe this would be your desired outcome. Otherwise, let me know if I misunderstood.

Other Things I've Noticed:

  • I'm not quite sure what purpose returning "empty" serves in your conditional situation.

  • In the nested for, I believe you may be breaking out of the loop prematurely.

  • Some of the string patterns can be simplified (e.g. [^%s]+ to %S, etc).

The logical operations on line 8 are also redundant; if a:find(b) is true then String:find(b) will always be false unless a and b are the same (in which case the or is still unnecessary).

All in all, I'd recommend a code clean-up. Clean code goes a long way in debugging a program. Try to simplify the concept as much as possible, then take a wack at the code.

Disclaimer

I'm proposing my code snippet above as a solution to your problem, but I'm still not quite sure if it still meets your criteria. If this wasn't what you were looking for, leave a comment describing how so and I will try to update my answer accordingly! Hope this helps!

1 comment

0

I’ve gotten to where I want by my own terms, but I realized this Logic would extract anything that counts for whichever word I want gone, I should’ve mentioned that I’m trying to remove a sentence. I.e it’ll remove any word for instance “Hi” if it’s in the table anywhere in the String, how could I restrict it to being strictly in the order of words? (apologies that might not make total sense)

This discussion is preserved as it was. New answers, comments, and votes are closed.