Writing (after reading) a student password file.

We here read the same student file as before, but now write the augmented file to a new file called, appropriately, `new_students_fall_96.dat'. In this first example we use a while construct for the write as well as for the read.
#!/usr/local/bin/perl -- -*- C -*-
#last_name+first_name+password, where '+' is the delimter
$infilename = "students_fall_96.dat";
$outfilename = "new_students_fall_96.dat";
open ( STUD_FILE , $infilename) ;
$i = 0;
	while(){
	chop;#we are here assuming that there will be a carriage return
#		at the end of each line, i.e., that we create the file using
#		an editor.
#		We may need to change this, since we can use students to
#		self-register.
	$single_student[$i] = $_;
	$p1 = index($single_student[$i],'+');
	$last_name[$i] = substr($single_student[$i],0,$p1-1);
	$p2 = index($single_student[$i],'+',p1+1);
	$first_name[$i] = substr($single_student[$i],$p1+1,$p2-1);
	$password[$i] = substr($single_student[$i],$p2+1);
	if($first_name[$i] ne ''){print "first_name = ",$first_name[$i],"\n";}
	$i++;
	};
close (STUD_FILE);
$single_student[$i] = "Mendelsohn+Felix+12345";
print "
single_student[$i] = ",$single_student[$i],"\n"; open (STUD_FILE, ">$outfilename"); $j = 0; while($j<$i+1){ print "\n single_student[$j] = ",$single_student[$j],"\n"; # print STUD_FILE $single_student[$j],"\n"; $j++; } close (STUD_FILE);

We here use a ``while'' construct to write out the entire file:
open (STUD_FILE, ">$outfilename");
$j = 0;
while($j<$i+1){ 
	print "\n single_student[$j] = ",$single_student[$j],"\n";
#	print STUD_FILE $single_student[$j],"\n";
$j++;
}
close (STUD_FILE);
Notice that we have commented out the write to file, and substituted a write to the screen, so that we can see what is going on, rather than pollute our disk space.
Click here to test the (above) code
In this second version of the same program, we use a for construct to do the writing.
#!/usr/local/bin/perl -- -*- C -*-
#last_name+first_name+password, where '+' is the delimter
$infilename = "students_fall_96.dat";
$outfilename = "new_students_fall_96.dat";
open ( STUD_FILE , $infilename) ;
$i = 0;
	while(){
	chop;#we are here assuming that there will be a carriage return
#		at the end of each line, i.e., that we create the file using
#		an editor.
#		We may need to change this, since we can use students to
#		self-register.
	@single_student = split(/\+/);
	$last_name[$i] = $single_student[0];
	$first_name[$i] = $single_student[1];
	$password[$i] = $single_student[2];
	if($first_name[$i] ne ''){print "first_name = ",$first_name[$i],"\n";}
	$i++;
	};
close (STUD_FILE);
$single_student[$i] = "Mendelsohn+Felix+12345";
print "\n single_student[$i] = ",$single_student[$i],"\n";
open (STUD_FILE, ">$outfilename");
for($j=0;$j<$i+1;$j++){ 
	print "\n single_student[$j] = ",$single_student[$j],"\n";
#	print STUD_FILE $single_student[$j],"\n";
}
close (STUD_FILE);
Most important is the line
open (STUD_FILE, ">$outfilename");
which associates the file handle with an output file, i.e., a file to be written. The single > means, if the file is there already, erase it, then create it. If the file isn't there, then create it. Thus, in this case the original file (if it existed) is overwritten.
If instead, we wished to append lines to the current file, we would need two >'s, i.e.,
open (STUD_FILE, ">>$outfilename");
Notice that if we only changed the > to >> in the above program, the file (which originally had two lines) would have 5 lines after executing the program once, 11 lines after executing the program again, etc., etc., etc..
In general, we will want to do the more dangerous procedure of re-writing the existing file, i.e., over writing it. This entails a risk, since we have no safety copy, but we can correct for that easily.
Click here to test the (above) code
Our last variation on this theme comes now, employing some of what we learned before, and introducing something new (borrowed and blue:-)?).
#!/usr/local/bin/perl -- -*- C -*-
#last_name+first_name+password, where '+' is the delimter
$infilename = "students_fall_96.dat";
$outfilename = "new_students_fall_96.dat";
open ( STUD_FILE , $infilename) ;
$i = 0;
	while(){
	chop;#we are here assuming that there will be a carriage return
#		at the end of each line, i.e., that we create the file using
#		an editor.
#		We may need to change this, since we can use students to
#		self-register.
	($last_name[$i],$first_name[$i],$password[$i]) = split(/+/);
	if($first_name[$i] ne ''){print "first_name = ",$first_name[$i],
				", last_name = ",$last_name[$i]," and password = ",$password[$i],"
";}
	$i++;
	};
close (STUD_FILE);
$single_student[$i] = "Mendelsohn+Felix+12345";
($last_name[$i],$first_name[$i],$password[$i]) = split(/+/,$single_student[$i]);
open (STUD_FILE, ">$outfilename");
$j = 0;
for($j=0;$j<$i+1;$j++){ 
	$single_student[$j] = $last_name[$j]."+".$first_name[$j]."+".$password[$j]."\n";
	print "\n single_student[$j] = ",$single_student[$j],"\n";
	print "\n student  [$j] = ",$first_name[$j]," ",$last_name[$j],"\n";
#	print STUD_FILE $single_student[$j],"\n";
}
close (STUD_FILE);
There are several changes here, First, we have the initial split:
	($last_name[$i],$first_name[$i],$password[$i]) = split(/+/);
which accomplishes the assignment in one fell swoop.
Next, we have the second split, which shows an alternative form when one is not dealing with the $_ variable, i.e., the last line just read:
($last_name[$i],$first_name[$i],$password[$i]) = split(/+/,$single_student[$i]);
where the second argument of the split function is the line to be split.
Next comes the concatenation operator:
	$single_student[$j] = $last_name[$j]."+".$first_name[$j]."+".$password[$j]."
";
where the `.' is the concatenation operator, joining strings (there's a `join' function which we will deal with later).
Functionally, we've altered nothing, just learned a bit more about operations in Perl.

Last revision/editing, July 23, 1997