Perl的數(shù)組(array)
返回
插入括號程序,insert_bracket.pl
use strict;
use File::Copy;
my(%t,$n);
copy("gb.txt","gb_tmp.txt") or die "Copy failed: $!";
open(IN,"gb_tmp.txt") or die "Can't open the file gb_tmp.txt.\n";
open(OUT,">gb.txt");
$t{NO} = 1;
@{ $t{english} } = ();
while(){
if ( $t{NO} == 1 ) {
print OUT $_;
if ( /^\W/ && $. > 1 ) {
$t{NO} = 2;
}
} else {
if ( /^\W/ ) {
if ( $#{ $t{english} } == 1 ) {
for $n ( 0 .. 1 ) {
print OUT $t{english}[$n];
}
} else {
print OUT '()',"\n";
print OUT $t{english}[0];
}
print OUT $_;
@{ $t{english} } = ();
} else {
push(@{ $t{english} }, $_);
}
}
}
close(IN);
split的用法
# test_split.pl
use strict;
my(%t,$n);
$t{a} = '11=12==13=14';
print "a=$t{a}\n";
@{ $t{b} } = split(/=/,$t{a});
for $n ( 0 .. $#{ $t{b} } ) {
print "NO=$n,value=>$t{b}[$n]\n";
}
print '-------------------------------',"\n";
@{ $t{b} } = split(/==/,$t{a});
for $n ( 0 .. $#{ $t{b} } ) {
print "NO=$n,value=>$t{b}[$n]\n";
}
print '-------------------------------',"\n";
@{ $t{b} } = split(/==|=/,$t{a});
for $n ( 0 .. $#{ $t{b} } ) {
print "NO=$n,value=>$t{b}[$n]\n";
}
__END__
運(yùn)行結(jié)果
a=11=12==13=14
NO=0,value=>11
NO=1,value=>12
NO=2,value=>
NO=3,value=>13
NO=4,value=>14
-------------------------------
NO=0,value=>11=12
NO=1,value=>13=14
-------------------------------
NO=0,value=>11
NO=1,value=>12
NO=2,value=>13
NO=3,value=>14
文件改名
use strict;
my(%t,@fld,$n);
open(IN,"tmp1.txt") or die "Can't open the file tmp1.txt";
while(){
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";
把一個目錄下的所有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";
}
把一個數(shù)組中的相同項目合并
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
把一行中的第一個項目放到最后
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() {
@fld = split;
$t{e1} = '';
for $n ( 1 .. $#fld ) {
$t{e1} .= $fld[$n] . ' ';
}
print OUT $t{e1},$fld[0],"\n";
}
close(IN);
close(OUT);
分解一個二層數(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ù)一個單子的零件數(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
返回
泸西县|
凤凰县|
万源市|
拉萨市|
华宁县|
郁南县|
乾安县|
雅安市|
楚雄市|
筠连县|
剑河县|
武山县|
洪洞县|
林西县|
温州市|
陵川县|
朔州市|
桃园县|
东安县|
普陀区|
民勤县|
新巴尔虎右旗|
延边|
兴海县|
肇源县|
定西市|
泰宁县|
象州县|
涟源市|
商洛市|
桂林市|
通许县|
浙江省|
安义县|
富锦市|
新平|
平乐县|
靖江市|
尼玛县|
鄂托克旗|
博乐市|