←  Ask a Pro

ComputerCraft | Programmable Computers for Minecraft


Exploding strings without delimiters?

zxci's Photo zxci 25 Nov 2015

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:

Bomb Bloke's Photo Bomb Bloke 25 Nov 2015

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).

zxci's Photo zxci 25 Nov 2015

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. ^^