|
Answer» So reading in file to array is easy since it automatically assumes read all to eof. But when writing back, I GUESS you also need to tell it to write back all to end of array to the file, and so do I need to create an incremental loop writing back each value from the array until I hit nul or is there a better method? perl has lots of quick ways to shorthand get the job done. I was originally thinking it would be happy assuming to write all values from the array back, but it writes nothing to import2.txt
Code: [Select]#!/usr/bin/perl # Read, Alter, Write, and Read and display to show change in alt file
open(DATA,"<import.txt") or die "Can't open data"; lines = <DATA>; close(DATA);
print lines[0]; print lines[1]; print lines[2]; print lines[3]; print lines[6];
$input = <STDIN>; lines[6] = $input;
print lines[0]; print lines[1]; print lines[2]; print lines[3]; print lines[6];
open(DATA,">import2.txt") or die "Can't open data"; lines = <DATA>; close(DATA);
open(DATA,"<import2.txt") or die "Can't open data"; lines = <DATA>; close(DATA);
print lines[0]; print lines[1]; print lines[2]; print lines[3]; print lines[6]; In import.txt I have the following test data
1 2 3 4 5 6 7 8 9 0
Was hoping I was going to be able to place data DELIMITED like 1,2,3,4,5,6,7,8,9,0 but maybe its more efficient for the data to be 1 character per line format. Probably should get into multidimensional arrays for data grouped like
1,2,3 4,5,6 7,8,9
Arrays are my weak spot right now with writing back from them. Reading in and passing data to them is easy. Years ago in C++ I remember something about incrementing to eof or in this case End Of Array to write data from array back to file.If the data does not fill the array, where is the logical end of the array? There needs to be some way to count the number of items in the array that are valid data items.output to a file would be something like this:
Code: [Select]open(DATA,">import2.txt") or die "cannot open import2"; print DATA lines
what you appear to be doing is USING perl shorthand to try to read from the file each time; you are ASSIGNING the file variable to the array, which results in the array being populated from the file.
|