|
|
to open a file, use the command where FILE is just a variable name (note: no $). file.txt is replaced by the name of whatever file it is you are opening. the statement above opens a file for reading. to write a new file, it would be open (FILE, ">file.txt");
and to append to the end of the file, it would be when you are done with whatever you are doing to the file, close it: close FILE; where FILE is again the variable name. to loop line by line through a file you are reading, you can put it in a while loop like this:
while ($record =
if you have formatted data (ie a bunch of fields separated by ::), you can
break that data into pieces. if there is the line
Joe::Shmoe::111-11-1111
you can get the different fields into their own variables using the split
command:
($first_name, $last_name, $ssn) = split ("::", $record);
where "::" is whatever the delimiting character(s) are, and $record is the
variable storing the line. the three variables in () on the left will
store the values. split breaks the data wherever your delimiting
characters are, and assigns them in order to the variables on the left. if
you have fewer varaibles than data items which are split up, the first
data items will get assigned to varaibles, and the rest just get lost.
to write out to a file, we use the print statement, almost like normal:
print FILE "whatever you are printing";
|