LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-24-2005, 05:09 PM   #1
blackman890
Member
 
Registered: Oct 2004
Location: Iceland
Posts: 94

Rep: Reputation: 15
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?
 
Old 03-24-2005, 06:31 PM   #2
ichrispa
Member
 
Registered: Mar 2005
Location: Dresden, Germany
Distribution: OpenSuse 11.2/3, Debian 5.0 , Debian 1.3.1, OpenBSD
Posts: 277

Rep: Reputation: 32
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)?
 
Old 03-24-2005, 07:43 PM   #3
blackman890
Member
 
Registered: Oct 2004
Location: Iceland
Posts: 94

Original Poster
Rep: Reputation: 15
heh, sorry to be such a n00b, i didn't understand a word you said

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

Last edited by blackman890; 03-24-2005 at 07:45 PM.
 
Old 03-24-2005, 08:25 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Are you trying to do this with a shell script, perl, python, C, or something else?
 
Old 03-24-2005, 10:49 PM   #5
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Rep: Reputation: 30
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
 
Old 03-25-2005, 09:48 AM   #6
blackman890
Member
 
Registered: Oct 2004
Location: Iceland
Posts: 94

Original Poster
Rep: Reputation: 15
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?

Last edited by blackman890; 03-25-2005 at 10:35 AM.
 
Old 03-25-2005, 03:40 PM   #7
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Rep: Reputation: 30
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)
 
Old 03-25-2005, 05:09 PM   #8
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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)
 
Old 03-25-2005, 10:30 PM   #9
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Rep: Reputation: 30
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.
 
Old 03-26-2005, 10:11 AM   #10
blackman890
Member
 
Registered: Oct 2004
Location: Iceland
Posts: 94

Original Poster
Rep: Reputation: 15
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
 
Old 03-28-2005, 02:11 AM   #11
ichrispa
Member
 
Registered: Mar 2005
Location: Dresden, Germany
Distribution: OpenSuse 11.2/3, Debian 5.0 , Debian 1.3.1, OpenBSD
Posts: 277

Rep: Reputation: 32
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?
 
Old 03-28-2005, 02:59 AM   #12
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Rep: Reputation: 30
In Linux,

man inet_network

for more detail


it is not really a similiar question to the first post, though...
 
Old 03-28-2005, 06:20 AM   #13
ichrispa
Member
 
Registered: Mar 2005
Location: Dresden, Germany
Distribution: OpenSuse 11.2/3, Debian 5.0 , Debian 1.3.1, OpenBSD
Posts: 277

Rep: Reputation: 32
concern strings, sorry though, you're right
Thx
 
Old 03-28-2005, 11:04 AM   #14
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lua embedding issues eucharn Programming 2 07-16-2008 05:36 PM
Seperating IPTABLES Logs TheRealDeal Linux - Security 5 02-26-2005 09:51 AM
C....Search a string for a string Scrag Programming 4 06-14-2004 05:15 PM
seperating values using perl pantera Programming 3 05-26-2004 01:31 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 02:39 PM


All times are GMT -5. The time now is 01:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration