![]() | การปรับปรุงแฟ้มด้วยเลขบรรทัด | ![]() |
| ข้อควรทราบ |
|
| การสร้างแฟ้ม แต่ไม่มีข้อมูล |
| Click here for run /perl/plupdt1.pl |
#!/usr/local/bin/perl print "Content-type:text/html\n\n"; $filename = join '',"/data1/hm/thaiall.com","/perl/plupddat.txt"; open(myfile,">$filename"); close(myfile); print "Create ok";โปรแกรมนี้จะสร้างแฟ้ม plupddat.txt ในห้อง perl ถ้าเคยมีข้อมูลอะไรอยู่ก็จะหายหมด แต่ให้ระวัง ถ้าแฟ้มที่มี ถูกส่งมาจาก ftp อาจมีปัญหา เรื่องเจ้าของ แต่ถ้าลบและสร้างขึ้นใหม่ก็จะไม่เห็นปัญหาอะไร ปัญหาที่อาจเกิดขึ้นคือ update ข้อมูลแล้วไม่เป็นผล |
| การลบแฟ้ม |
| Click here for run /perl/plupdt2.pl |
#!/usr/local/bin/perl
print "Content-type:text/html\n\n";
$filename = join '',"/data1/hm/thaiall.com","/perl/plupddat.txt";
unlink ("$filename");
print "Delete ok";
โปรแกรมนี้จะลบแฟ้ม plupddat.txt จากห้อง perlโดยไม่ต้องเตือนว่าจะมีแฟ้มอยู่ หรือไม่ |
| เพิ่มระเบียน ต่อท้ายระเบียนสุดท้าย |
|
จะไม่ขอกล่าวถึงการรับค่าจาก form เพื่อนำค่านั้น ส่งให้กับ perl แล้วนำไปเพิ่ม เป็นระเบียนใหม่ ผู้ศึกษาน่าจะนำมาต่อกันด้วยหลักการ Common sense ได้ Click here for run /perl/plupdt3.pl ตัวอย่างผลลัพธ์ Time is Wed Nov 3 00:25:44 1999 Time is Wed Nov 3 00:25:46 1999 Time is Wed Nov 3 00:28:38 1999 Time is Wed Nov 3 00:28:42 1999 |
#!/usr/local/bin/perl print "Content-type:text/html\n\n"; $filename = join '',"/data1/hm/thaiall.com","/perl/plupddat.txt"; open(myfile,">>$filename"); $lct = localtime(time); print myfile "Time is ",$lct,"\n"; close(myfile); print "Add new record ok";โปรแกรมนี้จะนำเวลาปัจจุบัน เขียนต่อท้ายบรรทัดเดิม เมื่อสั่ง run 1 ครั้งข้อมูลในแฟ้ม plupddat.txt ก็จะถูกเพิ่มอีก 1 บรรทัด ทำให้แฟ้มมีจำนวนบรรทัดเพิ่มขึ้น |
|
เรียกข้อมูลมาแสดง เพื่อจะได้ดูข้อมูล และผลการเปลี่ยนแปลงของแฟ้ม |
|
เรียกโปรแกรม perl ให้ไปอ่านแฟ้มมาแสดง Click here to read file |
#!/usr/local/bin/perl print"Content-type:text/html\n\n"; $filename = join '',"/data1/hm/thaiall.com","/perl/plupddat.txt"; print "<pre>"; open(myfile,"$filename"); @getrec = <myfile>; close(myfile); print @getrec;ผลของโปรแกรมนี้จะนำข้อมูลที่อยู่ใน plupddat.txt มาพิมพ์ทั้งหมด โดยไม่จัดรูปแบบ หรือแยกบรรทัด |
| เพิ่มระเบียน ตามเลขบรรทัด |
|
Click here for open this form sample <form method=post action=/perl/plupdt4.pl> <input type=text name=num><br> <input type=text name=txt><br> <input type=submit value=submit> <input type=reset value="clear it"> </form> |
โปรแกรมในส่วนของ plupdt4.pl เป็นดังนี้
#!/usr/local/bin/perl
$getid = <STDIN>;
@getrec = split /&/,$getid;
($n1,$v1) = split /=/,$getrec[0];
($n2,$v2) = split /=/,$getrec[1];
print"Content-type:text/html\n\n";
$filename = join '',"/data1/hm/thaiall.com","/perl/plupddat.txt";
print "<pre>";
open(myfile,"$filename");
@getrec = <myfile>;
close(myfile);
$cntrec = @getrec;
unlink("$filename");
open(myfile,">$filename");
for $i (0 .. $cntrec-1) {
$id = $i + 1;
if ($v1 eq $id) { print myfile $v2,"\n"; }
print myfile $getrec[$i];
}
close(myfile);
print "add new ok"
|
| แก้ไขระเบียน ตามเลขบรรทัด |
|
Click here for open this form sample <form method=post action=/perl/plupdt6.pl> <input type=text name=num><br> <input type=text name=txt><br> <input type=submit value=submit> <input type=reset value="clear it"> </form> |
โปรแกรมในส่วนของ plupdt6.pl เป็นดังนี้
#!/usr/local/bin/perl
$getid = <STDIN>;
@getrec = split /&/,$getid;
($n1,$v1) = split /=/,$getrec[0];
($n2,$v2) = split /=/,$getrec[1];
print"Content-type:text/html\n\n";
$filename = join '',"/data1/hm/thaiall.com","/perl/plupddat.txt";
print "<pre>";
open(myfile,"$filename");
@getrec = <myfile>;
close(myfile);
$cntrec = @getrec;
unlink("$filename");
open(myfile,">$filename");
for $i (0 .. $cntrec-1) {
$id = $i + 1;
if ($v1 eq $id) {
print myfile $v2,"\n"; } else {
print myfile $getrec[$i];
} # if
} # for
close(myfile);
print "edit ok"
|
| ลบระเบียน ตามเลขบรรทัด |
|
Click here for open this form sample <form method=post action=/perl/plupdt7.pl> <input type=text name=num><br> <input type=submit value=submit> <input type=reset value="clear it"> </form> |
โปรแกรมในส่วนของ plupdt7.pl เป็นดังนี้
#!/usr/local/bin/perl
$getid = <STDIN>;
@getrec = split /&/,$getid;
($n,$v) = split /=/,$getrec[0];
print"Content-type:text/html\n\n";
$filename = join '',"/data1/hm/thaiall.com","/perl/plupddat.txt";
print "<pre>";
open(myfile,"$filename");
@getrec = <myfile>;
close(myfile);
$cntrec = @getrec;
unlink("$filename");
open(myfile,">$filename");
for $i (0 .. $cntrec-1) {
$id = $i + 1;
if ($v ne $id) { print myfile $getrec[$i]; }
} # for
close(myfile);
print "delete ok"
|
| แบบฝึกหัด |
|
![]() |
+ ผู้สนับสนุน + รับผู้สนับสนุน |