#!/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);
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.
#!/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(Most important is the line){ 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);
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.
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..
#!/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(There are several changes here, First, we have the initial split:){ 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);
($last_name[$i],$first_name[$i],$password[$i]) = split(/+/);which accomplishes the assignment in one fell swoop.
($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.
$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).
Last revision/editing, July 23, 1997