Jump to content

Exploding strings without delimiters?

lua help command

2 replies to this topic

#1 zxci

  • Members
  • 17 posts

Posted 25 November 2015 - 06:37 AM

I've looked around for quite a while and found a ton of information about splitting strings WITH delimiters in LUA, but I can't find a morsel about doing it without. Here's some relevant backstory about why I want to do it:

Just out of boredom I'm writing a rudimentary networking API with lua (just to see if I can). Info is sent in packets, like normal IP packet based networking. Different information is stored in different areas of the packet. For example,


The red first bit indicates the starting bit for the packet.
I plan to insert another category here between red and blue for routing bits.
The blue second through ninth bits indicate the length of the packet's contents (the green bits, or everything after what will be expanded upon as the routing bits).
The green bits are then the information, in binary, within the packet.

For example, that packet says:
start 80 HELLOWORLD.

Now, what I need to do is take that and break it up into an array/table I can reference by index so I can parse the packet. I just don't know how to do that without making a gigantic if/then program.

One solution I know of would be using a simple string.find based function to explode it and insert a comma after every single bit, but wouldn't that be extremely inefficient? It'd double the length of every packet. Is there another way? :angry:

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 25 November 2015 - 07:29 AM

It sounds like you're looking for string.sub(): http://lua-users.org...LibraryTutorial

I assume you're already aware that you could just send your tables directly through your modems, without building and unpackaging strings at each end (in fact most data types are accepted).

#3 zxci

  • Members
  • 17 posts

Posted 25 November 2015 - 07:48 AM

Yah I figured that out and actually solved my problem while I was waiting for moderator approval. It came to me out of nowhere that I was wording my queries wrong the entire time. I was googling, "how to explode strings without delimiters", and google kept throwing delimiter at me. I realized, "how to iterate through a string" got me the desired results right away lol.

I found a string.sub method.

But ye, I know you can just send data. This is more of a personal challenge project. ^^

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users