This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
zxci's profile picture

Exploding strings without delimiters?

Started by zxci, 25 November 2015 - 05:37 AM
zxci #1
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,

1010100000110100001100101011011000110110001101111001000000111011101101111011100100110110001100100

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 #2
Posted 25 November 2015 - 07:29 AM
It sounds like you're looking for string.sub(): http://lua-users.org/wiki/StringLibraryTutorial

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