LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Find empty lines in array

Hello
Hop you can help me.
I wanna read data from a file. I've created a case (see picture). So long it works fine, but I always get two empty lines in my array after my data. How can I prevent these two empty lines? Has anyone an idea?

Thank you very much 🙂
0 Kudos
Message 1 of 6
(3,750 Views)
The way you read the file and decode it the last bytes of it will be read and decoded into empty array elements.

I would redesign the entire code; instead of reading one byte at a time and doing inserts and thus resizing the array continously (a no no...it creates copies of the data and is thus memory costly and slow, the bigger the array, the worse the effect ..it can be quite dramatic) just read the entire file using e.g. read characters and then use the spreadsheet string to array to get the array...

Alternatively; read the size of the file, calculate how large an array you will need, initialize a shift register with an array that size, read line by line and then use the replace function to put the data into the array (using replace is the thing to do, never
insert..that's only OK if the array is very small and the operation is not to be repeated often).

In the first solution you load all the data into memory in one read operation and then use additional memory for the array, it thus a bit more memory expensive than the latter where there whole file is not loaded into memory as a string first, but is put into the array at once...on the other hand it requires more read operations. Often the best approach is to mix the two; reada block of the file into memory, process it, then read another block etc...that way you optimize the performance by keeping both memory usage and the number of read operations down to an optimal level...but normally that kind of optimization is onlyrequired if the files are very large and/or many...
Message 2 of 6
(3,750 Views)
this illustrates Mads' first solution:
0 Kudos
Message 3 of 6
(3,750 Views)
Thank you both!

The example from the illustration works fine. But I get at least one more empty line after the reading. How can I prevent that at best?

Greetings Sebastian
0 Kudos
Message 4 of 6
(3,750 Views)
It seems that there could be tab or line return at the end of your data file that would generate this blank entry in the array. You could programmatically check to see if the element is blank and then use the delete from array function to remove the element.
0 Kudos
Message 5 of 6
(3,750 Views)
There is probably a Carriage return-line feed at the end that produces the empty element, just use string subset to remove the last two characters prior to converting it to an array...(read the file as a string and display it as code, then see what kind of charcters you have at the end and remove the ones you do not need). An alternative is to read the file size and then use the read characters function with the number of characters set to the total minus the number of bytes you want to exclude...
0 Kudos
Message 6 of 6
(3,750 Views)