1.

Solve : Split String in Lua?

Answer»

So I am attempting to split all of the arg[a] values (which are separated by a comma (,)) into argtb[a] = {x,y}. The issue is that with the current fuction I have, it is removing the negative signs, and thus passing on the wrong number.

What I have:
Code: [Select]-- btw: arg = {...}

function Seperate() -- Seperate args into X and Y Values
a = 1 + tonumber(modifyer)
repeat
b = {}
index = 1
for value in string.gmatch(arg[a],"%w+") do
b [index] = value
index = index + 1
end
print(arg[a])
print(b[1],b[2])
os.execute("pause")

-- x = (arg:sub(arg[a])) --This one I could not get to work.
-- y = arg[a]:sub((arg[a]:find(',')+1)) --This one worked.

x = b[1]
y = b[2]


_G["argtb" ..a] = {x,y} -- outputs to table argtb%a%
-- (as per Batch Scripting syntax)
-----
print(_G["argtb"..a][1],_G["argtb"..a][2])
-----
a = a + 1
until (arg[a] == nil)
a = nil
end

Arg examples:
Code: [Select]-41,0
3,-1
1,1
5,-6
-2,-3

What I am currently getting: (with input of "-5,-6")
x = 5
y = 6

Any help or explanations would be appreciated as I just started learning and am not quite sure I understand.
I dont see anything in this code snippet, HOWEVER in search for an explaination, this is all I could find in reference to LUA and this happening where Absolute Value is involved as linked here and shown below: http://lua-users.org/wiki/MathLibraryTutorial

Quote

math.abs
Return the absolute, or non-negative value, of a given value.
> = math.abs(-100)
100
> = math.abs(25.67)
25.67
> = math.abs(0)
0

*Is there an absolute value conversion happening somewhere else in the program that is not shown in this code snippet?

Only other thought is that through a string to numeric conversion it is looking at - and each value as not a whole, but 2 DIFFERENT pieces of string data in which string to numeric conversion is dropping the - and causing an absolute value condition to happen. I ran into issues a while back when trying to cast from string to numeric, maybe thats the issue that is going on here.

Something like this could have a band aid placed into the code to end up with negitive numbers, however anyone who knows how to fix this may grit their teeth stating its poor programming, but if you knew you were working with data in a range of say -2000 to +2000, you could work with 0 to 4000, and when the data is extracted with the offset to work with positive integers, you can have an offset removal once you have your integer to pass it back to the correct value that will show as negative.

So for example if we were working with -23, and want -23 to be the number vs 23 as you have now. You can offset everything into positive territory even when written to data file so that -23 is actually 1977 with 2000 equal to 0. In the data file you have 1977, 2009 for values -23, 9 and before the data is used you remove the 2000 offset with integers working with integers and you will get your negative value vs the absolute value type condition that is happening.

I have had to do stuff like this in the past with C++, its probably not the right way, but when running into issues casting from string to integer a few lines of code extra is a band aid, although as I said a master programmer would probably grit their teeth at such a band aid.Quote
So for example if we were working with -23, and want -23 to be the number vs 23 as you have now. You can offset everything into positive territory even when written to data file so that -23 is actually 1977 with 2000 equal to 0. In the data file you have 1977, 2009 for values -23, 9 and before the data is used you remove the 2000 offset with integers working with integers and you will get your negative value vs the absolute value type condition that is happening.

The value's I'm using are passed directly from the user into the program. So the user call it with 'graph.lua -5,-6' where arg[1] becomes "-5,-6". It was a good idea, but sadly cannot be implemented without first separating the values.

I believe I have found the issue, and sadly it's only solvable by someone with lua experiance.
Code: [Select]for value in string.gmatch(arg[a],"%w+") do
Is separating it by punctuation, and happens to include "-" as punctuation. I've messed around with %w+ to no avail (%w+ is suppose to specify how to separate the string) I will continue messing with it until I find something that works.


EDIT: Fixed it... All I needed to do was put a "-" in front of "%w+"... I feel really stupid now...
Thanks for pointing me in the right direction DaveLembke!Glad I was able to help some even though I have never worked with LUA

Did some research on it and LUA looks interesting. Going to read up some more on it. Thanks for pointing me towards LUA to check it out I use C++, C#, and Perl as my choice languages so going to read deeper into LUA to see what benefits there are that C++, C#, and Perl dont already have.

Quote
Lua is a fast language engine with small footprint that you can embed easily into your application. Lua has a simple and well documented API that allows strong integration with code written in other languages. It is easy to extend Lua with libraries written in other languages. It is also easy to extend programs written in other languages with Lua. Lua has been used to extend programs written not only in C and C++, but also in Java, C# , Smalltalk, Fortran, Ada, Erlang, and even in other scripting languages, such as Perl and Ruby.


and interesting that its used in games that I play. So I am interested in looking to see what role LUA is used in each of those, if its common knowledge to know and not classified by the makers.

Quote
Lua is a proven, ROBUST language
Lua has been used in many industrial applications (e.g., Adobe's Photoshop Lightroom), with an emphasis on embedded systems (e.g., the Ginga middleware for digital TV in Brazil) and games (e.g., World of Warcraft and Angry Birds). Lua is currently the leading scripting language in games. Lua has a solid reference manual and there are several books about it. Several versions of Lua have been released and used in real applications since its creation in 1993. Lua featured in HOPL III, the Third ACM SIGPLAN HISTORY of Programming Languages Conference, in June 2007.



Discussion

No Comment Found