Perl的数组(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(<IN>){ 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__ 运行结果 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(<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";

把一个目录下的所有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"; }

把一个数组中的相同项目合并

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"; # 程序执行结果 # 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(<IN>) { @fld = split; $t{e1} = ''; for $n ( 1 .. $#fld ) { $t{e1} .= $fld[$n] . ' '; } print OUT $t{e1},$fld[0],"\n"; } close(IN); close(OUT);

分解一个二层数组(用于数据库处理)

$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__ 输出执行结果 QTY==>50=30=80=70==80 QTY2==>50=30=80=70 QTY4==>50 QTY4==>30 QTY4==>80 QTY4==>70 QTY2==>80 QTY4==>80

数一个单子的零件数量(用于数据库处理)

$$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); } 结果: A=>3=4==5=6==7 length=>5
返回