
Re: Input and write to file
Thrawn wrote:
I am trying to understand the principle andconcepts of having input written to a text file for another script usage. For example, let's suppose I have to enter some sector numbers, I am stuck on how to tell the script to write each number into a text file where each sector is on its own line. Here is a snip of the code I have so far:
I based it on Promethius' gameopen script for the menu structure. I am not sure where to tell the script to write each number I enter into the file and to enter it on its own line.
The other question I have is will the "enter 0 to finish" interfere with this? Meaning if I press zero to finish it will enter the zero in the text file.
I'm not sure why you want to write inputted sector data to the same line number in a text file. This will create a huge amount of over head every time you input a sector number to 'add'.
There are two ways you can approach this --assuming I'm understanding you:
1- SetArray $SOMETHING SECTORS, and flag each element in the $SOMETHING Array TRUE, as data is inputted. Then. When the user indicates that he/she is done.. cycle through the array and write your file (putting empty lines where the respective array element is FALSE)
2- Or. A more practicle approach: DELETE the file at script start up (many ways to deal with this step depending on your objective), simply append new Sector numbers to the file as they're inputted, so you get something like:
23
4311
3
4922
5834
10657
654
18534
...then the other script can read that file to a $DATA Array, and cycle through that much smaller $DATA Array while ($IDX <= $DATA) ... where $IDX were to equal 4, $DATA[$IDX] would equal '4922' ..this requires a little more work, but is far more efficient.