﹝學習記錄﹞- Perl – 正規算式, split, 檔案與檔案代碼

 

@array = split (/\s+/, $line);

 

Open (HANDLE, $file); 開啟並讀取檔案

Close (HANDLE); 關閉檔案

 

函式Open()

Open (HANDLE, “<$file”); 開啟並讀取檔案

Open (HANDLE, “>$file”); 開啟並寫入檔案

Open (HANDLE, “>>$file”); 開啟並新增資料至檔案中

Open (HANDLE, “+<$file”); 可讀又可寫,會保留原先檔案內容

Open (HANDLE, “+>$file”); 可讀又可寫,會覆蓋原先檔案內容

Open (HANDLE, “+>>$file”); 可讀又可新增

 

執行結果範例:

==================================================

AREF Structure.....: CELL_A

Insert Point.......: 0,0

Number of Columns..: 10

Column Pitch.......: 30

Number of Rows.....: 20

Row Pitch..........: 55

==================================================

 

程式碼

#!/usr/bin/perl -w  # csh檔頭

use strict;

 

my $txtfile="./circle_A.ctxt";

open(FH, $txtfile)||die "Can't open $txtfile for reading!\n";

print "CTXT File $txtfile\n";

 

my @arr;

while(my $line=<FH>){

  if ($line=~/^AREF/){  # =~ 含括;

    $_=$line;s/\(|\,|\)/ /g;$line=$_;  #預設變數 $_

    @arr=split(/\s+/, $line);

  }

}

 

#for (my $i=0; $i <=$#arr; $i++){

#}

 

print '='x50,"\n";

print "AREF Structure.....: $arr[1]\n";

print "Insert Point.......: $arr[2],$arr[3]\n";

print "Number of Columns..: $arr[4]\n";

print "Column Pitch.......: $arr[5]\n";

print "Number of Rows.....: $arr[7]\n";

print "Row Pitch..........: $arr[9]\n";

print '='x50,"\n";

 

close(FH);

 

, , , , , ,

yunyun 發表在 痞客邦 PIXNET 留言(0) 人氣()