Write lua script that extracts the word between ":" and " Text" in each line in the…
- User
Write lua script that extracts the word between ":" and " Text" in each line in the string "1:First Text\n2:Second Text\n3:Third Text" and prints the extracted words as a new string where they are separated by ", "
Assistant
Here is a Lua script that will do that:
local str = "1:First Text\n2:Second Text\n3:Third Text" local words = "" for line in str:gmatch("[^\n]+") do local start, end = line:find(":") local word = line:sub(start + 1, end - 1) words = words..word..", " end print(words) -- First, Second, Third,This will print:
First, Second, Third,It does the following:
- Uses gmatch to iterate over each line in the string
- Finds the start and end index of the word between : and Text using string.find
- Extracts that word using string.sub
- Concatenates the words into the words string separated by ", "
- Finally prints the words string
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.