Delete punctuation out of an array

Does anyone know of a function usable in Sugarcube which will let me remove specific punctuation characters out of an array? At the end of the sequence in my current program, I drop a number of the variables in the program into an array with a specific order and then use a javascript export function to save them locally as a .csv, but I’m having an issue where strings are still encased in ""s and sub-arrays still have []'s in the output.

Example of bad output: [“test48067583”,2,2,[[“MedicalTile1”,“Dialysis”,“pos”,0,0],

Is there a way to parse this array prior to export and remove the excess punctuation, without removing what’s needed for the program to function? I also definitely need the commas, or at least to replace them with whitespace characters so the csv files are still usable. I have been doing this manually in excel with Search-Replace, but that’s not really tenable for large numbers of data files, likely in the hundreds or thousands.

Desired output: test48067583,2,2,MedicalTile1,Dialysis,pos,0,0,

Ideally this would not have to involve regexes, as I have never had any sort of luck managing to understand how those work. I keep finding myself in positions where I have just enough programming knowledge to get myself in trouble, but not to know what I’m doing.

I have to ask, what kind of data structure is the data is coming from? There are a couple of ways you could do that using JavaScript string and array methods, starting with the .flat() method, so knowing where the data is coming from makes it easier to give you the optimal code for that.

Hmm, that is a good question. I’m using this command to set the array that gets exported: <<set $x = [$participant,$randomSeed,$memCond,_tileListOutput,_tileListPhoneOutput,_pt]>>

So it would be a basic Sugarcube array variable, I think? Possibly a generic object? The first 3 vars put into the array are basic variables and the latter 3 are temporary arrays I set up earlier on the page that contain all the data values.

I’m then using Chapel’s File System macros to export them.
( )

If it’s using square brackets (i.e. [...]) as you show above, then it’s normally an array. A generic object is set up using curly brackets (i.e. {...}) instead, though you can access keys on a generic object using square brackets instead of using dot notation (e.g. using $obj["key"] instead of $obj.key, though in that example the two methods of accessing that key are functionally equivalent).

So, it looks like you’re just using primitives (i.e. undefined, Booleans, numbers, and strings) and arrays (which are a specific type of object).

In that case, you can just use the .toString() method to get that without the brackets or quote marks.

In other words, if you do this:

<<set $x = [["a", "b"], 1, 2]>>
<<set $txt = $x.toString()>>

then that will display this:


which I think is what you’re looking for.

Hope that helps! :smiley:

Oh, great, that works fine. I knew that there must be some function that would do that.