5

I am using Lua in World of Warcraft.

I have this string:

"This\nis\nmy\nlife."

So when printed, the output is this:

This
is
my
life.

How can I store the entire string except the last line in a new variable?

So I want the output of the new variable to be this:

This
is
my

I want the Lua code to find the last line (regardless of how many lines in the string), remove the last line and store the remaining lines in a new variable.

Thank you.

Jstation
  • 407
  • 4
  • 14
  • 3
    `new_str = old_str:gsub("\n[^\n]*$", "")` – Egor Skriptunoff Nov 15 '20 at 12:28
  • That's great Egor. Can you tell me how to store the first line of old_str into new_str too? – Jstation Nov 15 '20 at 13:06
  • 2
    `first_line = old_str:match("[^\n]*")` – Egor Skriptunoff Nov 15 '20 at 13:20
  • Thanks, Egor. I would like to mark this as a correct answer but I'm unable to mark a comment. If you wish to put your replies into an answer, I will mark it as correct. – Jstation Nov 15 '20 at 13:51
  • By convention a line is always terminated by a newline character, so the example string does not really have a final line to remove. @EgorSkriptunoff approach is great for the example, but depending on how the input string is created you could have `"This\nis\nmy\nlife.\n"`, where there really is a _line_ at the end of the string; then adjustments would be needed. Do you really want to remove the characters following the final newline of the string, or do you want to remove the last _line_ from the string? – ad absurdum Nov 15 '20 at 15:56
  • You didn't specify if all strings contain a newline, or if you need that test included. Also, you didn't show any attempt. – Doyousketch2 Nov 15 '20 at 16:16
  • 2
    `new_str = old_str:gsub("\n[^\n]*(\n?)$", "%1")` will solve the problem described by @exnihilo – Egor Skriptunoff Nov 15 '20 at 19:15

4 Answers4

4

So I found that Egor Skriptunoff's solutions in the comments worked very well indeed but I am unable to mark his comments as an answer so I'll put his answers here.

This removes the last line and stores the remaining lines in a new variable:

new_str = old_str:gsub("\n[^\n]*$", "")

If there is a new line marker at the end of the last line, Egor posted this as a solution:

new_str = old_str:gsub("\n[^\n]*(\n?)$", "%1")

While this removes the first line and stores the remaining lines in a new variable:

first_line = old_str:match("[^\n]*")

Thanks for your help, Egor.

Jstation
  • 407
  • 4
  • 14
1

Most efficient solution is plain string.find.

local s = "This\nis\nmy\nlife." -- string with newlines
local s1 = "Thisismylife." -- string without newlines

local function RemoveLastLine(str)
    local pos = 0 -- start position
    while true do -- loop for searching newlines
        local nl = string.find(str, "\n", pos, true) -- find next newline, true indicates we use plain search, this speeds up on LuaJIT.
        if not nl then break end -- We didn't find any newline or no newlines left.
        pos = nl + 1 -- Save newline position, + 1 is necessary to avoid infinite loop of scanning the same newline, so we search for newlines __after__ this character
    end
    if pos == 0 then return str end -- If didn't find any newline, return original string

    return string.sub(str, 1, pos - 2) -- Return substring from the beginning of the string up to last newline (- 2 returns new string without the last newline itself
end

print(RemoveLastLine(s))
print(RemoveLastLine(s1))

Keep in mind this works only for strings with \n-style newlines, if you have \n\r or \r\n easier solution would be a pattern.

This solution is efficient for LuaJIT and for long strings. For small strings string.sub(s1, 1, string.find(s1,"\n[^\n]*$") - 1) is fine (Not on LuaJIT tho).

Spar
  • 1,582
  • 9
  • 16
1

I scan it backward because it more easier to remove thing from back with backward scanning rather than forward it would be more complex if you scan forward and much simpler scanning backward

I succeed it in one take

function removeLastLine(str) --It will return empty string when there just 1 line
  local letters = {}
  for let in string.gmatch(str, ".") do --Extract letter by letter to a table
    table.insert(letters, let)
  end

  local i = #letters --We're scanning backward
  while i >= 0 do --Scan from bacward
    if letters[i] == "\n" then
      letters[i] = nil
      break
    end
    letters[i] = nil --Remove letter from letters table
    i = i - 1
  end
  return table.concat(letters)
end

print("This\nis\nmy\nlife.")
print(removeLastLine("This\nis\nmy\nlife."))

How the code work

  1. The letters in str argument will be extracted to a table ("Hello" will become {"H", "e", "l", "l", "o"})

  2. i local is set to the end of the table because we scan it from the back to front

  3. Check if letters[i] is \n if it newline then goto step 7

  4. Remove entry at letters[i]

  5. Minus i with 1

  6. Goto step 3 until i is zero if i is zero then goto step 8

  7. Remove entry at letters[i] because it havent removed when checking for newline

  8. Return table.concat(letters). Won't cause error because table.concat return empty string if the table is empty

Blanket Fox
  • 377
  • 4
  • 15
-4
#! /usr/bin/env lua

local serif = "Is this the\nreal life?\nIs this\njust fantasy?"
local reversed = serif :reverse()  --  flip it

local pos = reversed :find( '\n' ) +1  --  count backwards
local sans_serif = serif :sub( 1, -pos )  --  strip it

print( sans_serif )

you can oneline it if you want, same results.

local str = "Is this the\nreal life?\nIs this\njust fantasy?"
print(  str :sub( 1,  -str :reverse() :find( '\n' ) -1 )  )

Is this the
real life?
Is this

Doyousketch2
  • 2,060
  • 1
  • 11
  • 11