LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Lua - seperating a string (https://www.linuxquestions.org/questions/programming-9/lua-seperating-a-string-305642/)

blackman890 03-24-2005 04:09 PM

Lua - seperating a string
 
Hi guys :)

i am having a small problem with my code, the problem is this:

i have a string, that has this value:
Code:

$List value1$$value2$$value3$$....$$
but i need to split them up, and check each and one of them separetly.

Can anyone help me?

ichrispa 03-24-2005 05:31 PM

Hey,
I'm currently having a similar problem. If you want to split your string up you have to memcpy (string.h) this string to a character array first, that is of course if this string is initialized by a pointer (as char *pointertostring).
An example. You have the array char txt[5] and the string char *original_text, which is equal to "Hello". If you memcpy(&txt, original_text, 7); then "H" would be txt[0]m "e" txt[1] and so on. Keep in mind that each string is terminated by a null character ("\0"), so you have to copy to more elements than your string is long...
Once you've done this you can analyze your string piece by ptece or rather element by element.
However, and this is where I am stuck at... , most functions expect the "char *pointertostring", thus a constant, as an argument.
Let's say that I have the values "192.168.0.1" stored in an array and need to make a constatant out of it to send it to inet_ntoa. Does anybody know how I can do this (parse it perhaps..didn't work though)?

blackman890 03-24-2005 06:43 PM

heh, sorry to be such a n00b, i didn't understand a word you said :p

EDIT
i don't know what each value is long, that is the problem :-/

Dark_Helmet 03-24-2005 07:25 PM

Are you trying to do this with a shell script, perl, python, C, or something else?

ahwkong 03-24-2005 09:49 PM

If you do it by perl, it is going to be very easy.

You can take a look at the function split() in perl. You can specify $$ as the seperator.

Of course, some work need to be done to remove the $List bit

blackman890 03-25-2005 08:48 AM

so i can't do it in Lua, i must do it in Perl?


EDIT
function split() what will the output of that function be?
i mean, it i use $$ as pattern, then what will i get out of it?

ahwkong 03-25-2005 02:40 PM

Dear, dear,...

I guess none realised that "Lua" is a programming language. You should make it clear in your first post, not just putting mysterious 'Lua' in the heading.

I have no idea how popular it is. But I guess Lua is likely a language popularly used for teaching purpose. (Modula-2 and Haskell are languages of this category too)

I am afriad I am helping you to do your homework...

The less people use it, the less likely you can get help form LQ. You may be better off using Lua mailing.list. But then the lecturers are watching :-)

Always check the documentation. It takes me just a few min to find out:

http://www.lua.org/pil/7.1.html

Near the end of that page, there is an answer to your question (Of course, you need some slight modification)

deiussum 03-25-2005 04:09 PM

Lua scripting is used in custom WoW interfaces, and many other things. I didn't realize before WoW, but it is actually used in quite a number of applications, and they have a C library that lets you add lua scripting functionality to your own applications.

I've just started looking at it a bit myself, but you might want to take a look at the string handling functions of Lua 5.0 here. (the version used by WoW, if that is what you are using Lua for)

ahwkong 03-25-2005 09:30 PM

I revisted the web site after your post, and find that there are many project makes use of Lua (http://www.lua.org/uses.html). Impressive.

I had jumped too quickly into conclusion about the domain of Lua previously.

blackman890 03-26-2005 09:11 AM

Thanks for the help guys :)

ahwkong
Sorry but this is not a homework :) my friend is running a server, and he asked me if there was a command in the server which list's all registered users (since there are both unregistered and registered)

But since there aren't any, i told him i would create a script that does the trick :)

the Server supports Lua so i decided to give it a chance :)

thanks for the help man, i am now trying to see if my modification on that link you gave me will work :)

ichrispa 03-28-2005 01:11 AM

Interresting :).
Let's revert to C++ . Since my question is similar, though apparently in a different language, I will not start a seperate thread but simply refrase my prior question:
I have a character array (constant), which needs to be casted into whatever char *pointerToString represents. Usually this would be the address of the first item of a character array named pointerToString.
The problem is that no function (particullarly inet_ntoa) accepts a character array, even if I present it with a pointer to the first item. So, how do I get arround this problem?

ahwkong 03-28-2005 01:59 AM

In Linux,

man inet_network

for more detail


it is not really a similiar question to the first post, though...

ichrispa 03-28-2005 05:20 AM

concern strings, sorry though, you're right
Thx

deiussum 03-28-2005 10:04 AM

Quote:

Originally posted by ahwkong
I revisted the web site after your post, and find that there are many project makes use of Lua (http://www.lua.org/uses.html). Impressive.

I had jumped too quickly into conclusion about the domain of Lua previously.

I also was impressed that so many things made use of Lua, some of them quite familiar like Baldur's Gate. I was suprised that I had never heard about it before.


All times are GMT -5. The time now is 09:49 PM.