Perl的散列(hash)

返回

修改零件的数量

use strict; my(%t,$n,$n1,$n2); $t{type1id} = '245==332'; $t{partsid} = '3=2=4==2=8'; $t{QTY} = '30=20=40==20=80'; $t{type1id2} = '245==332'; $t{partsid2} = '4==2=8'; $t{QTY2} = '40==20=80'; @{ $t{QTYnew} } = qw/300 200 400 200 800/; @{ $t{ttt1} } = split(/==/,$t{type1id}); @{ $t{ppp1} } = split(/==/,$t{partsid}); $t{NO}=0; @{ $t{qqq3} } = (); for $n ( 0 .. $#{ $t{ttt1} } ) { $t{ttt2} = $t{ttt1}[$n]; $t{ppp2} = $t{ppp1}[$n]; @{ $t{ppp3} } = split(/=/,$t{ppp2}); @{ $t{qqq1} } = (); for $n1 ( 0 .. $#{ $t{ppp3} } ) { $t{NO}++; $t{ppp4} = $t{ppp3}[$n1]; $t{tp_qty}{$t{ttt2}}{$t{ppp4}} = $t{QTYnew}[$t{NO}-1]; push(@{ $t{qqq1} },$t{QTYnew}[$t{NO}-1]); } $t{qqq2} = join('=',@{ $t{qqq1} }); push(@{ $t{qqq3} },$t{qqq2}); } $t{qqq4} = join('==',@{ $t{qqq3} }); print "enq1 result:\n"; print "old==>$t{QTY}\n"; print "new==>$t{qqq4}\n\n"; @{ $t{ttt1} } = split(/==/,$t{type1id2}); @{ $t{ppp1} } = split(/==/,$t{partsid2}); $t{NO}=0; @{ $t{qqq3} } = (); for $n ( 0 .. $#{ $t{ttt1} } ) { $t{ttt2} = $t{ttt1}[$n]; $t{ppp2} = $t{ppp1}[$n]; @{ $t{ppp3} } = split(/=/,$t{ppp2}); @{ $t{qqq1} } = (); for $n1 ( 0 .. $#{ $t{ppp3} } ) { $t{NO}++; $t{ppp4} = $t{ppp3}[$n1]; push(@{ $t{qqq1} },$t{tp_qty}{$t{ttt2}}{$t{ppp4}}); } $t{qqq2} = join('=',@{ $t{qqq1} }); push(@{ $t{qqq3} },$t{qqq2}); } $t{qqq4} = join('==',@{ $t{qqq3} }); print "enq2 result:\n"; print "old==>$t{QTY2}\n"; print "new==>$t{qqq4}\n\n"; enq1 result: old==>30=20=40==20=80 new==>300=200=400==200=800 enq2 result: old==>40==20=80 new==>400==200=800

# 必须置零,因为下一台主机的DWG极有可能同名!

@{ $t{plist}{id}{$t{dwg1}} } = (); @{ $t{plist}{name}{$t{dwg1}} } = (); @{ $t{plist}{code}{$t{dwg1}} } = (); @{ $t{plist}{QTY}{$t{dwg1}} } = (); @{ $t{plist}{Nuid}{$t{dwg1}} } = ();

把复数的enq2价格归并到一个enq1

$t{enq2s} = $self->dbh->selectrow_array("SELECT enq2s FROM enq1 WHERE id = $t{quo2_id}"); @{ $t{enq2_ids} } = split(/=/,$t{enq2s}); for $n ( 0 .. $#{ $t{enq2_ids} } ) { $t{enq2_id} = $t{enq2_ids}[$n]; ($t{type1id},$t{partsid},$t{price}) = $self->dbh->selectrow_array("SELECT type1id,partsid,price FROM enq2 WHERE id = $t{enq2_id}"); @{ $t{tts} } = split(/==/,$t{type1id}); @{ $t{pps} } = split(/==/,$t{partsid}); @{ $t{pps2} } = split(/=/,$t{price}); $t{NO} = 0; for $n1 ( 0 .. $#{ $t{tts} } ) { $t{tts1} = $t{tts}[$n1]; $t{pps1} = $t{pps}[$n1]; @{ $t{pps1s} } = split(/=/,$t{pps1}); for $n2 ( 0 .. $#{ $t{pps1s} } ) { $t{NO}++; $t{pps1s1} = $t{pps1s}[$n2]; $t{list}{$t{tts1}}{$t{pps1s1}} = $t{pps2}[$t{NO}-1]; } } } # enq1 @{ $t{prices} } = (); ($t{type1id},$t{partsid}) = $self->dbh->selectrow_array("SELECT type1id,partsid FROM enq1 WHERE id = $t{quo2_id}"); @{ $t{tts} } = split(/==/,$t{type1id}); @{ $t{pps} } = split(/==/,$t{partsid}); for $n1 ( 0 .. $#{ $t{tts} } ) { $t{tts1} = $t{tts}[$n1]; $t{pps1} = $t{pps}[$n1]; @{ $t{pps1s} } = split(/=/,$t{pps1}); for $n2 ( 0 .. $#{ $t{pps1s} } ) { $t{pps1s1} = $t{pps1s}[$n2]; push(@{ $t{prices} },$t{list}{$t{tts1}}{$t{pps1s1}}); } } $t{price0} = join("=",@{ $t{prices} }); #$t{price0} = $t{list}{"154"}{"2"}; $t{sql} = 'UPDATE quo2 set price0 = "'; $t{sql} .= $t{price0} . '" where id = '; $t{sql} .= $t{quo2_id}; $t{DO} = $self->dbh->do($t{sql});

通过enq2->quo1找出原价

$t{sth} = $self->dbh->prepare("select id, enq1id from enq2"); $t{sth}->execute; while (@rec = $t{sth}->fetchrow_array) { if ( $rec[1] == $$pref{id} ) { $t{NO} = 0; $t{enq2_id} = $rec[0]; # 取出价格 $t{pri} = $self->dbh->selectrow_array("SELECT price FROM quo1 WHERE id = $t{enq2_id}"); @{ $t{pris} } = split(/=/,$t{pri}); ($t{tt1},$t{pp1}) = $self->dbh->selectrow_array("SELECT type1id,partsid FROM enq2 WHERE id = $t{enq2_id}"); @{ $t{tt2} } = split(/==/,$t{tt1}); @{ $t{pp2} } = split(/==/,$t{pp1}); for $n ( 0 .. $#{ $t{tt2} } ) { $t{tt3} = $t{tt2}[$n]; $t{pp3} = $t{pp2}[$n]; @{ $t{pp4} } = split(/=/,$t{pp3}); for $n1 (0 .. $#{ $t{pp4} } ) { $t{NO}++; $t{list}{$t{tt3}}{$t{pp4}[$n1]} = $t{pris}[$t{NO}-1]; } } } } $t{sth}->finish;

>=误写成==的BUG(弄了一天才发现)

foreach $e (@a,@b) { $count{$e}++ }; #@union = sort {$a<=>$b} keys %count; @union = sort keys %count; foreach $e ( keys %count ) { # if ($count{$e} == 2 ) { # 正好两个的情况(不对),这个==不对 if ($count{$e} >= 2 ) { # 应该是>= $count2{$e}++; } } for $n ( 0 .. $#b ) { next if $count2{$b[$n]}; $t{idmax}++; push @diff, $b[$n]; $t{enq1_ids}{$b[$n]} = $t{idmax}; }

删除重复的项目并排序

use strict; my(%t,$n,@fld); # 读取main_maker1_order2.txt文件 open(IN,"main_maker1_order2.txt") or die "Can't open the file main_maker1_order2.txt.\n"; while(<IN>){ next if $. == 1; chop; @fld = split(/==>/); $t{list}{$fld[1]}++; } close(IN); # 读取makers_tmp2.txt文件 open(IN,"makers_tmp2.txt") or die "Can't open the file makers_tmp2.txt.\n"; while(<IN>){ chop; $t{list}{$_}++; } close(IN); # 排序操作 @{ $t{orders} } = sort keys %{ $t{list} }; open(OUT,">makers.txt"); print OUT 'Filename=makers.txt',"\n"; $t{NO} = 0; for $n ( 0 .. $#{ $t{orders} } ) { $t{NO}++; $t{N1} = sprintf("%05d",$t{NO}); $t{line} = $t{N1} . '==>' . $t{orders}[$n]; print OUT $t{line},"\n"; } close(OUT); __END__ ################################################################### # 把所有的小写字母改成大写字母并排序 open(IN,"makers_tmp.txt") or die "Can't open the file makers_tmp.txt.\n"; while(<IN>){ chop; $t{line} = uc($_); $t{list}{$t{line}}++; # 删除相同的项目 } close(IN); # 排序操作 @{ $t{orders} } = sort keys %{ $t{list} }; open(OUT,">makers_tmp2.txt"); for $n ( 0 .. $#{ $t{orders} } ) { print OUT $t{orders}[$n],"\n"; } close(OUT); ###################################################################

数据库操作的一个程序,不用了。留作存档

# 从enq1取出主机编号(type1id),零件号码(partsid),数量(QTY) ($t{type1id},$t{partsid},$t{QTY}) = $self->dbh->selectrow_array("SELECT type1id,partsid,QTY FROM enq1 WHERE id = $t{enq1_id}"); @loop1 = (); $t{NO} = 0; @{ $t{type1id_list} } = split(/==/,$t{type1id}); @{ $t{partsid_list} } = split(/==/,$t{partsid}); @{ $t{QTY_list} } = split(/==/,$t{QTY}); # Table的一行是一个项目 for $n ( 0 .. $#{ $t{type1id_list} } ) { $t{type1id1} = $t{type1id_list}[$n]; $t{partsid1} = $t{partsid_list}[$n]; $t{QTY1} = $t{QTY_list}[$n]; # 从main_type1中取出主机名和DWG图号 ($t{id1},$t{type1},$t{DWG}) = $self->dbh->selectrow_array("select id, name,DWG from main_type1 where id = $t{type1id1}"); # 从零件名表中取出零件编号和图纸号 @{ $t{pid_list} } = split(/=/,$t{partsid1}); @{ $t{Q_list} } = split(/=/,$t{QTY1}); @{ $t{DWGs} } = split(/=/,$t{DWG}); # 生成零件表名,根据enq1的零件编号从数据库取出零件信息和所属图纸号 $t{ptable} = sprintf("%06d",$t{type1id1}); $t{ptable} = 'a' . $t{ptable}; @{ $t{dwgs1} } = (); for $n1 ( 0 .. $#{ $t{pid_list} } ) { $t{pid1} = $t{pid_list}[$n1]; $t{Q1} = $t{Q_list}[$n1]; @{ $t{p1} } = $self->dbh->selectrow_array("select * from $t{ptable} where id = $t{pid1}"); $t{dwg1} = $t{p1}[4]; push(@{ $t{plist}{id}{$t{dwg1}} },$t{p1}[0]); push(@{ $t{plist}{name}{$t{dwg1}} },$t{p1}[1]); push(@{ $t{plist}{code}{$t{dwg1}} },$t{p1}[2]); push(@{ $t{dwgs1} },$t{dwg1}); } # 合并重复的图纸号==>这个操作充分利用了Perl散列的特性 %seen = (); @{ $t{dwgs2} } = (); foreach $item (@{ $t{dwgs1} }) { unless ( $seen{$item} ) { $seen{$item} = 1; push(@{ $t{dwgs2} },$item); } } # 第一层:主机名 # 第二层:图纸号(XXXDWG设定为不知道图纸号) # 第三层:零件名 # 把数据放入HTML的TABLE的TR for $n1 ( 0 .. $#{ $t{dwgs2} } ) { $t{dwg1} = $t{dwgs2}[$n1]; $t{DWG1} = $t{DWGs}[$t{dwg1}-1]; # 取出图纸号 $t{line1} = '<tr bgcolor="#FFF000" align="center"><td colspan=7>'; $t{line1} .= $t{id1} . '==>' . $t{DWG1}; $t{line1} .= '</td></tr>'; my %row = ( line1 => $t{line1} ); push(@loop1, \%row); # 处理零件 for $n2 ( 0 .. $#{ $t{plist}{id}{$t{dwg1}} } ) { $t{NO}++; # enq1的所有Parts的编号 $t{pid1} = $t{plist}{id}{$t{dwg1}}[$n2]; $t{name1} = $t{plist}{name}{$t{dwg1}}[$n2]; $t{code1} = $t{plist}{code}{$t{dwg1}}[$n2]; $t{line1} = '<tr bgcolor="#F0FFF0" align="center"><td>'; $t{line1} .= $t{NO} . '</td><td>'; $t{line1} .= $t{name1} . '</td><td>'; $t{line1} .= $t{code1} . '</td><td>'; $t{line1} .= $t{code1} . '</td><td>'; $t{line1} .= $t{code1} . '</td><td>'; $t{line1} .= $t{code1} . '</td><td>'; $t{line1} .= $t{code1}; $t{line1} .= '</td></tr>'; my %row = ( line1 => $t{line1} ); push(@loop1, \%row); } # 必须置零,因为下一台主机的DWG极有可能同名! $t{plist}{id}{$t{dwg1}} = (); $t{plist}{name}{$t{dwg1}} = (); $t{plist}{code}{$t{dwg1}} = (); } }
返回