Char arrays

I want to define the equivalent of an array of chars that I can index into to read and write… for example

char my_array[20];
my_array[5] = ‘A’;

I’ve tried this with a string, but each character is not individually accessible.

Thanks
Anand

Take a look at the blob class here

It’s not exactly the same as what you want but you can accesss it directly through the [ ] operator.

Strings are immutable in Squirrel so you can’t get into the middle of one unless you create a new one by using slice() to get the two parts and concatenating it with “A” using +

sort of like (not tested or guaranteed not to explode)

myNewShinyArray <- my_array.slice(0,5) + "A" + my_array.slice(5);

would insert A

myNewShinyArray <- my_array.slice(0,5) + "A" + my_array.slice(6);

should replace element 5 with A

Which doesnt’ seem like what you want at all :slight_smile:

Thanks for your help… The documentation for blob is shocking, could you happen to be able to point me to a usage example ?

Thanks
Anand

Hi Anand,

Yeah, the squirrel docs can be a little spartan at first. Here’s a quick example of initializing and writing to a blob:

Create a 20-byte blob
local myBlob = blob(20);

A blob is just like a file handle, so if you just write from here, you’ll start at the first byte and write to the end. To write at a specific offset, you have to seek to that offset. Here, let’s seek to an offset of 5 from the beginning of the blob:

myBlob.seek(5,'b');

You can write many different data types - see the docs on “readn” and “writen” to see the different types you can write. Here we’ll write an 8-bit unsigned value (at offset 5, remember):

myBlob.writen(0xBEAF, 'b');

Write another (now at offset 6)
myBlob.writen(0xB000, 'b');

Write a 32-bit float at offset 7
myBlob.writen(3.1415, 'f');

…you can also directly index like this, for single bytes:

myBlob[5]=‘b’;

Thanks for your support :

Still having trouble with regards to this:
`
line1 = blob(40);
line2 = blob(40);

write_ptr = 0;
line_ptr = 0;

read_ptr = 0;

function ClearDisplay()
{
write_ptr = 0;
line_ptr = 0;
for (i=0; i <40; i++)

{
	line1[i] = 0x00;
	line2[i] = 0x00;
}

}`

the above is causing this error : “ERROR: the index ‘line1’ does not exist”

I’ve tried… ::line1[i], this.line1[i], root.line[i] but all report the same error.

Thanks
Anand

The error is on the first occurrence of “line1”, not the one in the loop.

When creating new variables, new slots in the root table, you need to use
line1 <- blob(40);
and likewise for all your other variables. This sort-of saves you from typos where you try to assign to an existing variable but mistype the name and accidentally silently create a new one instead.

Peter