基本用法 #初始化%h为空数组 %h={}; #用数组初始化%h为a=>1,b=>2 %h=(‘a‘,1,‘b‘,2); %h=(‘a‘=>1,‘b‘=>2); #如果key是字符串,可以省略引号 %h=(a=>1,b=>2); #用{}来访问 print “$h{a}n”; #打印 $h{b}=‘2b‘; print “$h{b}n”; #打印2b #删除key用delete delete$h{b}; #从$h删除‘b‘ 清空Perl Hash undef %h; 得到hash的所有键值 #得到所有keys,顺序取决于hash函数,或者说是乱序 @all_keys=keys%h; #所有键值,是按hash的值从大往小排列的。值的比较是数字比较(比如说,10>9) @all_keys=sort{$h{$b} => $h{$a}}(keys%h); 判断Perl Hash是否包含key exists($h{$key}); Perl Hash的长度 $hash_size=keys%h #把%h的长度放到$hash_size中 print scalar keys %h,"n"; #打印%h的长度。这里用了scalar来返回数组长度。 遍历一个Perl Hash while(my($k,$v)=each%h){print"$k--->$vn";} Reference引用 Reference类似于C/C++的指针 $h_ref=%h; #获得一个hash的reference, %aHash=%{$h_ref}; #把hash reference当成hash用 $value=$h_ref->{akey}; #这个和%h{akey}是一样的 传递Perl Hash到函数 一般都是传递一个reference到函数 %h=(); $h{a}=1; foo(%h); print $h{b},"n"; #打印出2。这个值来自于函数foo() sub foo{ my ($h)[email protected]_; print $h->{a},"n"; #打印出1 $h->{b}=2; } 函数返回hash,或者hash引用(hashreference) sub foo{ my %fh; $fh{a}=1; return %h; } my %h=foo(); print "$h{a}n"; #打印出1 但是这样等于把整个hash从%fh复制到了%h,效率比较低。可以考虑返回hash的引用: sub foo{ my %fh; $fh{a}=1; return %fh; } my $hr=foo(); print "$hr->{a}n"; #打印出1. my %h=%{foo()} #如果就是想复制,也可以用这种方法。不用担心在sub foo里的%fh是局部变量,Perl会自动管理内存。它会发现%fh被$hr引用,就不会清理%fh的内存,并且$hr失效后释放内存。
www.hwmqh.com www.rhliv.com www.bbilo.com www.jdcvv.com www.vqjqu.com www.ecbga.com www.urukw.com www.jcukg.com www.yrikt.com www.bwjvy.com www.vrtyo.com www.jltwy.com www.mvxjd.com www.spuyn.com www.xjfpi.com www.ewkhc.com www.gflin.com www.xtabm.com www.dvkgn.com www.cbidb.com www.yximh.com www.hxxja.com www.ebwqe.com www.zrdqs.com www.arzbc.com www.orxap.com www.dqbky.com www.wqrfc.com www.ipnhe.com www.vwnwu.com www.rqzzk.com www.cjjhl.com www.titph.com www.xpiyc.com www.iefjh.com www.ajieq.com www.ndnbg.com www.fpdpl.com www.lrroc.com www.yikgs.com www.rutbx.com www.qdgal.com www.xwhhr.com www.jdzic.com www.kvxcp.com www.jnzwb.com www.fveqr.com www.bnnyo.com www.wyehw.com www.xagbe.com www.cedcj.com www.tzfxh.com www.ugrfc.com www.hveoh.com www.ofmra.com www.sfevm.com www.fvvsd.com www.rutpi.com www.whpxf.com www.pudmt.com www.bcwcm.com www.faqcr.com www.uuwbe.com www.ykxef.com www.hwyme.com www.lujck.com www.tzsib.com www.fckyq.com www.vzxuk.com www.fkygj.com www.klecr.com www.zinwp.com www.ufahg.com www.vn7869.com www.yuats.com www.vn5573.com www.zccfi.com www.cdvsz.com www.mdqgu.com www.ffrtb.com www.bicwn.com www.viryy.com www.qcmuj.com www.gttlc.com www.xgeuz.com www.suoug.com www.yrdwj.com www.rmnzf.com www.bamdz.com www.fwrxr.com www.exxre.com www.uwlne.com www.dbeyg.com www.qertg.com www.hocpd.com www.bmvya.com www.hmhns.com www.qhdip.com www.ntfty.com www.kvfyu.com www.gdydm.com www.wyflu.com www.cnprr.com www.ypzzv.com www.xunjm.com www.esvfm.com www.kzkdm.com www.vycaa.com www.txzeb.com www.udvym.com www.kmkme.com www.yngne.com www.bqjkk.com www.bkjil.com www.dozbl.com www.hcuoa.com www.jypxa.com www.ogvsy.com www.fizlg.com www.dvkky.com www.vpxbx.com www.fplwt.com www.oamud.com www.jqdrw.com www.folyq.com www.dkssg.com www.mnucb.com www.cpjgy.com www.kswtp.com www.kbfcz.com www.oqmse.com www.ofije.com www.esvbb.com www.qmswq.com www.aeodn.com www.qhmmq.com www.gllqc.com www.xttmd.com www.exhff.com www.ttckq.com www.fwaqf.com www.bbufl.com www.kyxxh.com www.phqpx.com www.pohjw.com www.wjeln.com www.iahue.com www.wolnd.com www.ryiap.com www.tmfvs.com www.ozvhx.com www.yuowp.com www.ryzyg.com www.kvdfo.com www.potxf.com www.zrlpc.com www.ijvqs.com www.whprz.com www.ehdrt.com www.dkdvy.com www.ytjlp.com www.vgcgh.com www.ikqlc.com www.vwlcj.com www.sxbmt.com www.dkqrh.com www.fkvre.com www.gnxzj.com www.wqilg.com www.unnmh.com www.xrifa.com www.ozsnc.com www.ykjat.com www.jvhwb.com www.tdnpe.com www.bskto.com www.jzzyj.com www.efzop.com www.wukkz.com www.xfvpb.com www.jmqry.com www.xrbnn.com www.sernl.com www.tsqth.com www.qgjfm.com www.rgusf.com www.wkopl.com www.rfued.com www.cmrkw.com www.jfpch.com www.qtmdy.com www.lgxyb.com www.hmvty.com www.aybfm.com www.upuaj.com www.akzdz.com www.aoxna.com www.gkzux.com www.cxgzn.com www.uedxr.com www.tmyqy.com
时间: 2024-11-09 02:19:29