[3.5.0-rc.1] Information on fileread() function
-
Just having a look at the use of the new fileread() function.
I'm not sure what the intended use case is (So I'm probably doing something for which it was intended), but on first glance it seems it would be perfect for storage of arrays for future re-use.
However as implemented I see a couple of issues for doing that...- When using the echo command to write an array to the file, the curly braces are written to the file, which is incompatible with fileread()
- When using the echo command to write an array to the file, string values are not enclosed in double quotes
- Boolean and null values in the array are incompatible with fileread(), so a bit of work is required to add an empty field instead of a null
Trying to test for booleans typically gave an error such as "cannot convert to same type" so I ended up just wrapping everything in double quotes.
If I don't use the line that wraps the values in double quotes, I get an error when reading it back invar myArray = {3,"true",null,4.87,12.76,"false",3,"string2",} echo var.myArray var myString= "" while iterations < #var.myArray if var.myArray[iterations] = null set var.myString = var.myString ^ "," else set var.myString = var.myString ^ """" ^ var.myArray[iterations] ^ """" ^ "," ;set var.myString = var.myString ^ var.myArray[iterations] ^ "," echo >"0:/macros/conditional_g_code_macros/myArray.csv" var.myString var readBackArray = fileread("0:/macros/conditional_g_code_macros/myArray.csv",0,#var.myArray,',') echo var.readBackArray[#var.myArray-1] = "string2"
Which results in a file containing (if I wrap in double quotes)
"3","true",,"4.87","12.76","false","3","string2",
And a result of
OR
If I don't wrap all the values in double quotes
A file of3,true,,4.87,12.76,false,3,string2,
and a result of
So I guess, for me it would be ideal if (wishlist)
- There was a function to write an array to file which stripped the curly braces
- That function wrote nulls and an empty value
- Fileread() allowed for booleans
-
@OwenD The usecase initially is to read a CSV into an array. As you have observed it looks like further work would be needed to format echo strings to provide a suitable input. You might have some success using the echo >>> function though? I have not tested that.
-
@T3P3Tony
Thanks Tony
Using echo>>> would mean multiple disk writes, so I elected to build a string first.
Short of string handling routines this is a viable workaround for dynamic creation of a csv file.
I'm sure the function works perfectly for the intended purpose.
These are definitely just observations and wish lists, not complaints