2

For example, I have arbitrary lines in this format:

directory C:\Program Files\abc\def\

or something like.

log-enabled On

I want to be able to extract the "C:\Program Files\ab\def\" part out of that first line. Likewise, I want to extract the "On" out in the second line. The spaces between the variable and its value are arbitrary. I will know the name of the variable, but I need extract the value based on that.

So basically, I want to remove the first word and a number of arbitrary spaces that follow the first word, and return what remains until the end of the line.

1
  • 5
    BTW: you have a lot of questions with quite a few answers. You may want to go through them and accept some of them where appropriate. Commented Aug 21, 2012 at 19:47

3 Answers 3

4

Assuming that, by "word" you mean "a string of characters without spaces", you can do this:

for line in ioFile:lines() do
  local variable, value = line:match("(%S+)%s+(.+)")
  ... --Do stuff with variable and value
end
Sign up to request clarification or add additional context in comments.

1 Comment

Hi. I thin this should work. So if I have a variable for the line.
0

One alternative with string.match was shown by Nicol Bolas, here is another alternative:

function splitOnFirstSpace(input)
    local space = input:find(' ') or (#input + 1)
    return input:sub(1, space-1), input:sub(space+1)
end

Usage:

local command, param = splitOnFirstSpace(line)

If no argument is given (splitOnFirstSpace('no-param-here')), then param is the empty string.

2 Comments

would this work for arbitrary number of spaces in between the variables and value?
No, it would not. You would need to call input:find('%s+') and save both start and end location of the white space. Or, you could trim the parameters afterwards.
-1

I do not believe Lua is packaged with a split() function like Ruby or Perl.

I found that this guy built a lua version of Perl's split function: http://lua-users.org/lists/lua-l/2011-02/msg01145.html

If you can guarantee that the argument will only have 1 word before it, with that word not containing any spaces, you can read in that line, run the split function on it, and use the return array's 1 index value as what you want.

You could error check that too and make sure you get a 'C:\' within your expected directory, or check to make sure the string is == to 'On' or 'Off'. Because of using the hardcoded index value I really advocate you error check your expected value. Nothing is worse than having an assumed value be wrong.

If an error is detected make sure to log or print it to the screen so you know about it.

This could catch bugs where maybe the string that was input is improper.

Some simple code that models what I suggest you do:

line = "directory C:\Program Files\abc\def/";

contents = line.split(" "); --Split using a space
directory = contents[2]; --Here is your directory

if(errorCheckDir(directory))
--Use directory
end

EDIT: In response to comments below Lua indeed begins indexing at 1, not 0. Also, in the case that the directory contains spaces (which is probable) instead of simply using contents[2], I would loop through all of contents except index 1, and piece back together the directory making sure to add the required space between each index that you attach.

So in the case above, contents[2] and contents[3] would have to be stitched back together with a space in between to recover the proper directory.

directory = contents[2].." "..contents[3]

This can be easily automated using a function which has a loop in it and returns back the proper directory:

function recoverDir(contents)
    directory = "";
    --Recover the directory
    for i=2, table.getn(contents) do
        directory = directory..contents[i].." ";
    end
    --strip extra space on the end
    dirEnd = string.len(directory);
    directory = string.sub(directory,1,dirEnd-1);
    return directory; --proper directory
end

3 Comments

And what if the directory has spaces, such as in his example?
Does not work for spaces; and lua tables are indexed from one.
You both are totally right. @Nicol I could modify the above to just loop through everything except the first result of the split and we could recover the directory name, but that would be messy, and I would just go with your solution instead Daurn: Totally forgot about Lua having that behavior. Thanks for pointing that out. I will edit the above to reflect these changes just so that I do not confuse people totally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.