perl 學(xué)習(xí)資料整理篇
更新時(shí)間:2008年10月02日 00:04:23 作者:
比較多也亂了點(diǎn),大家先看看吧
--------------------------------------------------------------------------------
返回
Perl的數(shù)組(array)
返回
--------------------------------------------------------------------------------
文件改名
use strict;
my(%t,@fld,$n);
open(IN,"tmp1.txt") or die "Can't open the file tmp1.txt";
while(<IN>){
if (/^site/) {
@fld = split;
push(@{ $t{list} },$fld[0]);
}
}
close(IN);
for $n ( 0 .. $#{ $t{list} } ) {
$t{NO} = $n + 1;
$t{NO} = sprintf("%02d",$t{NO});
$t{filem} = 'sitem' . $t{NO} . '.htm';
$t{filenew} = 'site' . $t{NO} . '.htm';
system("rename $t{filem} $t{filenew}");
print "$t{filem}==>$t{filenew}\n";
}
exit;
for $n ( 0 .. $#{ $t{list} } ) {
$t{file1} = $t{list}[$n];
$t{NO} = $n + 1;
$t{NO} = sprintf("%02d",$t{NO});
$t{filem} = 'sitem' . $t{NO} . '.htm';
$t{filenew} = 'site' . $t{NO} . '.htm';
print "$t{file1}==>$t{filem}\n";
system("rename $t{file1} $t{filem}");
}
print "\n";
把一個(gè)目錄下的所有jpg文件改名
my(%t,@list,$n);
@list = glob("*.jpg");
for $n ( 0 .. $#list ) {
$t{old_file} = $list[$n];
$t{e1} = sprintf("%02d",$n);
$t{new_file} = 'e' . $t{e1} . '.jpg';
system("rename $t{old_file} $t{new_file}");
print "$t{new_file}<==$t{old_file}\n";
}
把一個(gè)數(shù)組中的相同項(xiàng)目合并
use strict;
my(@list,%seen,@uniq,$item);
@list = (3,3,3,2,2,4,4,4,4);
%seen = ();
@uniq = ();
print"list=@list\n";
foreach $item (@list) {
unless ( $seen{$item} ) {
$seen{$item} = 1;
push(@uniq,$item);
}
}
print"uniq=@uniq\n";
# 程序執(zhí)行結(jié)果
# list=3 3 3 2 2 4 4 4 4
# uniq=3 2 4
把一行中的第一個(gè)項(xiàng)目放到最后
use strict;
my(%t,$n,@fld);
open(IN,"tmp3.txt") or die "Can't open the file tmp3.txt\n";
open(OUT,">tmp4.txt");
while(<IN>) {
@fld = split;
$t{e1} = '';
for $n ( 1 .. $#fld ) {
$t{e1} .= $fld[$n] . ' ';
}
print OUT $t{e1},$fld[0],"\n";
}
close(IN);
close(OUT);
分解一個(gè)二層數(shù)組(用于數(shù)據(jù)庫處理)
$t{QTY} = '50=30=80=70==80';
print "QTY==>$t{QTY}\n";
@{ $t{QTY1} } = split(/==/,$t{QTY});
for $n ( 0 .. $#{ $t{QTY1} } ) {
$t{QTY2} = $t{QTY1}[$n];
print ' ',"QTY2==>$t{QTY2}\n";
@{ $t{QTY3} } = split(/=/,$t{QTY2});
for $n1 ( 0 .. $#{ $t{QTY3} } ) {
$t{QTY4} = $t{QTY3}[$n1];
print ' ',"QTY4==>$t{QTY4}\n";
}
}
__END__
輸出執(zhí)行結(jié)果
QTY==>50=30=80=70==80
QTY2==>50=30=80=70
QTY4==>50
QTY4==>30
QTY4==>80
QTY4==>70
QTY2==>80
QTY4==>80
數(shù)一個(gè)單子的零件數(shù)量(用于數(shù)據(jù)庫處理)
$$ref{A} = '3=4==5=6==7';
print "A=>$$ref{A}\n";
($ref) = get_length($ref);
print "length=>$$ref{NO}\n";
sub get_length {
my ($ref) = @_;
my (%t,$n);
@{ $t{As} } = split(/=/,$$ref{A});
$$ref{NO} = 0;
for $n ( 0 .. $#{ $t{As} } ) {
$t{A1} = $t{As}[$n];
if ( $t{A1} != 0 ) {
$$ref{NO}++;
}
}
return ($ref);
}
結(jié)果:
A=>3=4==5=6==7
length=>5
--------------------------------------------------------------------------------
返回
利用HTML::Template模塊
戻る
--------------------------------------------------------------------------------
生成互相鏈接的復(fù)數(shù)個(gè)HTML文件
# 通過這個(gè)程序,把幾百行的數(shù)據(jù)生成HMLT表格
# multi.pl
use strict;
use HTML::Template;
my(%t,@fld,$n,$template,@loop);
print "Please input filename=";
chop($t{root}=<STDIN>);
$t{tmpl} = 'index.html';
$t{inputf} = $t{root} . '.txt';
open(IN,"names.txt") or die "Can't open the file names.txt.\n";
while(<IN>){
if ( /^NAME\s/ ) {
@fld = split;
$t{list}{$fld[1]} = $fld[2];
}
}
close(IN);
$template = HTML::Template->new(filename => $t{tmpl});
@loop = ();
$t{htmfile} = $t{root} . '.htm';
$t{flag} = 1;
open(IN,"$t{inputf}") or die "Can't open the file $t{inputf}";
while(<IN>){
next if $. == 1; # 跳過第一行
next if length($_) < 2; # 最后的空行也跳過
if ( $t{flag} == 1 ) { # 第一組數(shù)據(jù)
$t{flag} = 2;
push(@{ $t{N1s} },$_);
$t{N11} = $_;
} elsif ($t{flag} == 2) { # 第二組數(shù)據(jù)
$t{clist}{$t{N11}} = $_;
$t{flag} = 3;
} elsif ($t{flag} == 3) { # 第三組數(shù)據(jù)
$t{elist}{$t{N11}} = $_;
$t{flag} = 1;
}
}
close(IN);
# 按第一組數(shù)據(jù)排序
@{ $t{NN} } = sort {lc($a) cmp lc($b)} @{ $t{N1s} };
# 為了檢查輸入數(shù)據(jù)的錯(cuò)誤,第一次運(yùn)行是最好不排序
#@{ $t{NN} } = @{ $t{N1s} };
for $n ( 0 .. $#{ $t{NN} } ) {
$t{N1} = $t{NN}[$n];
$t{c1} = $t{clist}{$t{N1}};
$t{e1} = $t{elist}{$t{N1}};
$t{count}{$t{N1}}++;
if ( $t{count}{$t{N1}} > 1 ) { # 這個(gè)操作是為了防止重復(fù)
next;
}
my %row = (
N1 => $t{N1},
C1 => $t{c1},
E1 => $t{e1}
);
push(@loop, \%row);
}
$t{etitle} = uc($t{root});
$template->param(std_loop => \@loop);
$template->param(ename => $t{etitle});
$template->param(cname => $t{list}{$t{etitle}});
open(OUT,">$t{htmfile}");
print OUT $template->output;
close(OUT);
print "The output file is $t{htmfile}\n";
__END__;
filename:names.txt
NAME ANSI 美國
NAME BS 英國
NAME DIN 德國
NAME EN 歐洲
NAME GB 中國
NAME ISO ISO
NAME JIS 日本
NAME NF 法國
<table width=75% align="center" border=1 cellpadding=5>
<tr bgcolor="#3399FF" align="center"><th width=20%>編號(hào)</th><th width=40%>中文名稱</th><th width=40%>英文名稱</th></tr>
<TMPL_LOOP NAME="std_loop">
<tr bgcolor="lightcyan" align="left"><td><TMPL_VAR NAME="N1"></td><td><TMPL_VAR NAME="C1"></td><td><TMPL_VAR NAME="E1"></td></tr>
</TMPL_LOOP>
</table>
一氣生成數(shù)百個(gè)HTML文件
# make_html.pl
use strict;
use HTML::Template;
my(%t,@fld,$n,$template,@loop,$h_ref);
print "Please input the directory name=";
chop($t{root}=<STDIN>);
$$h_ref{dir} = 'vF' . $t{root};
$t{inputf} = $t{root} . '_vF.csv';
open(IN,"./$$h_ref{dir}/$t{inputf}") or die "Can't open the file /$$h_ref{dir}/$t{inputf}.\n";
while(<IN>){
next if ( $. == 1 );
chop;
@fld = split(/,/);
next unless $fld[1];
$t{T1} = sprintf("%10.6f",$fld[0]);
push(@{ $$h_ref{Time} },$t{T1});
push(@{ $$h_ref{k_files} },$fld[1]);
push(@{ $$h_ref{Start} },$fld[2]);
}
close(IN);
$t{tmpl} = 'output0.htm';
$t{htmfile} = 'index.html';
$template = HTML::Template->new(filename => $t{tmpl});
opendir(DIR,"$$h_ref{dir}") or die "Can't opendir $$h_ref{dir}: $!";
@loop = ();
$t{N1} = 0;
for $n ( 0 .. $#{ $$h_ref{Time} } ) {
$t{N1}++;
$t{Time1} = $$h_ref{Time}[$n];
$t{file1} = $$h_ref{k_files}[$n];
$t{Start1} = $$h_ref{Start}[$n];
$t{csv1} = '<a href="' . $t{file1} . '">' . $t{file1} . '</a>';
$t{file1} =~ s/csv/xls/;
$t{xls1} = '<a href="' . $t{file1} . '">' . $t{file1} . '</a>';
$t{file1} =~ s/xls/htm/;
$t{gif1} = '<a href="' . $t{file1} . '">' . $t{file1} . '</a>';
my %row = (
N1 => $t{N1},
Time => $t{Time1},
csv => $t{csv1},
xls => $t{xls1},
gif => $t{gif1},
Start => $t{Start1}
);
push(@loop, \%row);
}
$template->param(loop => \@loop);
$template->param(dir => $$h_ref{dir});
open(OUT,">./$$h_ref{dir}/$t{htmfile}");
print OUT $template->output;
close(OUT);
# 這個(gè)循環(huán)可一氣生成指定數(shù)目的HTML文件
for $n ( 0 .. $#{ $$h_ref{Time} } ) {
$$h_ref{file1} = $$h_ref{k_files}[$n];
($h_ref) = make_vhtm($h_ref);
}
close(IN1);
print "Finished.\n";
sub make_vhtm {
my($h_ref) = @_;
my(%t,$n,$template1,@loop);
$$h_ref{file1} =~ s/csv/htm/;
$t{htmfile} = $$h_ref{file1};
$template1 = HTML::Template->new(filename => "v000000.htm");
$template1->param(htm => $t{htmfile});
$$h_ref{file1} =~ s/htm/gif/;
$template1->param(gif => $$h_ref{file1});
open(OUT1,">./$$h_ref{dir}/$t{htmfile}");
print OUT1 $template1->output;
close(OUT1);
return($h_ref);
}
1;
__END__;
錯(cuò)誤信息
$template = HTML::Template->new(filename => $$h_ref{tmpl},option => "$$h_ref{NO}");
-------------------------------------
Please input the directory name=1_2_1
The output file is ./vF1_2_1/index.html
HTML::Template->new() called with odd number of option parameters - should be of
the form option => value at make_html.pl line 78
--------------------------------------------------------------------------------
戻る
opendir
戻る
--------------------------------------------------------------------------------
input.pl(該程序的要點(diǎn)是使用opendir)
# input.pl
use strict;
use HTML::Template;
my(%t,@fld,$n,$template,@loop);
$t{tmpl} = 'input0.htm';
$t{htmfile} = 'index.html';
$template = HTML::Template->new(filename => $t{tmpl});
print "Please input the directory name=";
chop($t{dir}=<STDIN>);
opendir(DIR,"$t{dir}") or die "Can't opendir $t{dir}: $!";
while ( defined($t{file}=readdir(DIR)) ) {
next if $t{file} =~ /^\.\.?$/; # skip . and ..
if ( substr($t{file},-3) eq 'csv' ) {
$t{NO1} = $t{file};
substr($t{NO1},-4) = '';
substr($t{NO1},0,9) = '';
$t{list}{$t{NO1}} = $t{file};
}
}
close(DIR);
@loop = ();
$t{N1} = 0;
for $n ( sort {$a<=>$b} keys %{ $t{list} } ) {
$t{N1}++;
$t{file} = $t{list}{$n};
$t{N3} = '<a href="' . $t{file} . '">' . $t{file} . '</a>';
my %row = (
N1 => $t{N1},
N2 => $n,
file => $t{N3}
);
push(@loop, \%row);
}
$template->param(loop => \@loop);
$template->param(dir => $t{dir});
open(OUT,">./$t{dir}/$t{htmfile}");
print OUT $template->output;
close(OUT);
print "The output file is ./$t{dir}/$t{htmfile}\n";
__END__;
--------------------------------------------------------------------------------
戻る
# color_index.pl
use strict;
use HTML::Template;
my(%t,@fld,$n,$template,@loop);
print "Please input filename=";
chop($t{root}=<STDIN>);
$t{tmpl} = $t{root} . '0.htm';
$t{inputf} = $t{root} . '.txt';
$template = HTML::Template->new(filename => $t{tmpl});
@loop = ();
$t{htmfile} = $t{root} . '1.htm';
$t{flag} = 1;
open(IN,"$t{inputf}") or die "Can't open the file $t{inputf}";
while(<IN>){
next if $. == 1;
next if length($_) < 2;
chop;
if ( $t{flag} == 1 ) {
$t{flag} = 2;
push(@{ $t{N1s} },$_);
$t{N11} = $_;
} elsif ($t{flag} == 2) {
$t{clist}{$t{N11}} = $_;
$t{flag} = 3;
} elsif ($t{flag} == 3) {
$t{elist}{$t{N11}} = $_;
$t{flag} = 1;
}
}
close(IN);
#@{ $t{NN} } = sort @{ $t{N1s} };
@{ $t{NN} } = @{ $t{N1s} };
for $n ( 0 .. $#{ $t{NN} } ) {
$t{N1} = $t{NN}[$n];
$t{c1} = $t{clist}{$t{N1}};
$t{e1} = $t{elist}{$t{N1}};
$t{content} = $t{N1} . '<br>' . $t{c1} . '<br>' . $t{e1};
$t{c11} = substr($t{c1},2,2);
$t{c12} = substr($t{c1},4,2);
$t{c13} = substr($t{c1},6,2);
$t{c14} = substr($t{c1},8,2);
$t{c1} = '#' . $t{c14} . $t{c13} . $t{c12} . $t{c11};
$t{color1} = '<td bgcolor="' . $t{c1} . '"> </td>';
$t{content1} = '<td>' . $t{content} . '</td>';
push(@{ $t{colors} },$t{color1});
push(@{ $t{contents} },$t{content1});
}
$t{C1} = 8;
$t{C4} = 1;
$t{line1} = $t{line2} = 0;
for $n ( 0 .. $#{ $t{colors} } ) {
$t{color1} = $t{colors}[$n];
$t{content1} = $t{contents}[$n];
$t{C2} = int($n/$t{C1});
$t{C3} = abs($n/$t{C1}-int($n/$t{C1}));
if ( $t{C2} > $t{C4} ) {
$t{C4}++;
}
if ( $t{C3} < 0.0000001 ) {
if ( !($t{line1}) ) {
$t{line1} = '<tr>' . $t{color1};
} else {
$t{line1} .= '</tr>';
push(@{ $t{lines} },$t{line1});
$t{line1} = '<tr>' . $t{color1};
}
} elsif ( $n == 55 ) {
$t{line1} .= $t{color1} . '</tr>';
push(@{ $t{lines} },$t{line1});
} else {
$t{line1} .= $t{color1};
}
if ( $t{C3} < 0.0000001 ) {
if ( !($t{line2}) ) {
$t{line2} = '<tr>' . $t{content1};
} else {
$t{line2} .= '</tr>';
push(@{ $t{lines} },$t{line2});
$t{line2} = '<tr>' . $t{content1};
}
} elsif ( $n == 55 ) {
$t{line2} .= $t{content1} . '</tr>';
push(@{ $t{lines} },$t{line2});
} else {
$t{line2} .= $t{content1};
}
}
for $n ( 0 .. $#{ $t{lines} } ) {
$t{line1} = $t{lines}[$n];
my %row = (
line1 => $t{line1}
);
push(@loop, \%row);
}
$template->param(loop => \@loop);
open(OUT,">$t{htmfile}");
print OUT $template->output;
close(OUT);
---------------------------------------------------
ColorIndex
1
&H000000
RGB(0,0,0)
53
&H003399
RGB(153,51,0)
52
&H003333
RGB(51,51,0)
51
&H003300
RGB(0,51,0)
49
&H663300
RGB(0,51,102)
11
&H800000
RGB(0,0,128)
55
&H993333
RGB(51,51,153)
56
&H333333
RGB(51,51,51)
9
&H000080
RGB(128,0,0)
46
&H0066FF
RGB(255,102,0)
12
&H008080
RGB(128,128,0)
10
&H008000
RGB(0,128,0)
14
&H808000
RGB(0,128,128)
5
&HFF0000
RGB(0,0,255)
47
&H996666
RGB(102,102,153)
16
&H808080
RGB(128,128,128)
3
&H0000FF
RGB(255,0,0)
45
&H0099FF
RGB(255,153,0)
43
&H00CC99
RGB(153,204,0)
50
&H669933
RGB(51,153,102)
42
&HCCCC33
RGB(51,204,204)
41
&HFF6633
RGB(51,102,255)
13
&H800080
RGB(128,0,128)
48
&H969696
RGB(150,150,150)
7
&HFF00FF
RGB(255,0,255)
44
&H00CCFF
RGB(255,204,0)
6
&H00FFFF
RGB(255,255,0)
4
&H00FF00
RGB(0,255,0)
8
&HFFFF00
RGB(0,255,255)
33
&HFFCC00
RGB(0,204,255)
54
&H663399
RGB(153,51,102)
15
&HC0C0C0
RGB(192,192,192)
38
&HCC99FF
RGB(255,153,204)
40
&H99CCFF
RGB(255,204,153)
36
&H99FFFF
RGB(255,255,153)
35
&HCCFFCC
RGB(204,255,204)
34
&HFFFFCC
RGB(204,255,255)
37
&HFFCC99
RGB(153,204,255)
39
&HFF99CC
RGB(204,153,255)
2
&HFFFFFF
RGB(255,255,255)
17
&HFF9999
RGB(153,153,255)
18
&H663399
RGB(153,51,102)
19
&HCCFFFF
RGB(255,255,204)
20
&HFFFFCC
RGB(204,255,255)
21
&H660066
RGB(102,0,102)
22
&H8080FF
RGB(255,128,128)
23
&HCC6600
RGB(0,102,204)
24
&HFFCCCC
RGB(204,204,255)
25
&H800000
RGB(0,0,128)
26
&HFF00FF
RGB(255,0,255)
27
&H00FFFF
RGB(255,255,0)
28
&HFFFF00
RGB(0,255,255)
29
&H800080
RGB(128,0,128)
30
&H000080
RGB(128,0,0)
31
&H808000
RGB(0,128,128)
32
&HFF0000
RGB(0,0,255)
相關(guān)文章
Perl遍歷目錄和使用Linux命令分析日志的代碼實(shí)例分享
這篇文章主要介紹了Perl遍歷目錄和使用Linux命令分析日志的代碼分享,需要的朋友可以參考下2014-05-05用perl實(shí)現(xiàn)生物突變的隨機(jī)模擬程序代碼
perl寫的生物突變的隨機(jī)模擬程序,有需要的朋友可以參考下2013-03-03perl 變量 $/ 的用法解析 上下文為行模式時(shí),$/ 定義以什么來區(qū)分行
默認(rèn)狀態(tài)下,很顯然都是用\n來區(qū)分行,\n也被我們稱作為換行符。當(dāng)讀取序列時(shí),按行來讀取時(shí),就是以換行符為標(biāo)準(zhǔn)2013-03-03- fdupe 是一個(gè)很小的 Perl 腳本,用來檢索指定目錄并找出其中重復(fù)的文件,該腳本是通過文件內(nèi)容來識(shí)別是否重復(fù)文件,而非文件名。fdupe 無需其他 Perl 腳本支持,運(yùn)行速度非???/div> 2013-03-03
perl去除重復(fù)內(nèi)容的腳本代碼(重復(fù)行+數(shù)組重復(fù)字段)
perl去除重復(fù)內(nèi)容的小腳本,有需要的朋友可以參考下,包括重復(fù)行、數(shù)據(jù)組中重復(fù)的字段2013-03-03perl中srand()與time的函數(shù)使用方法介紹
這篇文章主要介紹了perl中srand與time函數(shù)的使用,需要的朋友可以參考下2013-03-03最新評(píng)論