ABC of Perl programming

download ABC of Perl programming

If you can't read please download the document

description

Fundamental of Perl programming

Transcript of ABC of Perl programming

2. Perl ABC Data StructureData StructureScalarListHashReferenceFilehandleFunction 3. Perl ABC Data StructureScalarA numberA stringA referenceListA list is ordered scalar data.HashAssociative arrays 4. Perl ABC Data StructureList examplesA list is ordered scalar data.#+begin_srcperl@a=("fred","barney","betty","wilma");#ugh!@a=qw(fredbarneybettywilma);#better!@a=qw(fredbarneybettywilma);#samething#+end_src 5. Perl ABCData StructureHash examples"associative arrays"#+begin_srcperl #+begin_srcperl%words=(%words=qw(fred=>"camel",fredcamelbarney=>"llama",barneyllamabetty=>"alpaca", bettyalpacawilma=>"alpaca", wilmaalpaca););#+end_src #+end_src 6. Perl ABC Data StructureHashcontinue #+begin_srcperl @words=qw( fredcamel barneyllama bettyalpaca wilmaalpaca ); %words=@words;#+end_src 7. Perl ABC Data StructureSpecial Things Nested ListThere is NOT anythig like list of lists#+begin_srcperl @a=qw(fredbarneybettywilma); @b=("tom",@a,"jerry");#becareful #whatyouactuallygetis @b=qw(tomfredbarneybettywilmajerry);#+end_src 8. Perl ABC Data StructureSpecial Things Nested ListBut there is nested list in the real worldWhat you really mean is#+begin_srcperl @a=qw(fredbarneybettywilma); @b=("tom",[@a],"jerry"); @b=("tom",@a,"jerry");#+end_src#+begin_srcperl $c=[@a];$c=@a; @b=("tom",$c,"jerry");#+end_src 9. Perl ABCData StructureSpecial Things Nested HashThere is nested hash in the real world#+begin_srcperl$words_nest{mash}={captain=>"pierce",%words_nest=(major=>"burns",fred=>"camel",barney=>"llama", corporal=>"radar",};betty=>"alpaca",wilma=>"alpaca",jetsons=>{husband=>"george",wife=>"jane","hisboy"=>"elroy",#Keyquotesneeded.},);#+end_src 10. Perl ABC Data AccessData AccessAccess Scalar DataAccess List DataAccess Hash Data 11. Perl ABC Data AccessData AccessAccess Scalar DataAccess List DataAccess Hash Data 12. Perl ABCData AccessScalar$fred="pay";$fredday="wrong!";$barney="Its$fredday";#notpayday,but"Itswrong!"$barney="Its${fred}day";#now,$barneygets"Itspayday"$barney2="Its$fred"."day";#anotherwaytodoit$barney3="Its".$fred."day";#andanotherway 13. Perl ABC Data AccessData AccessAccess Scalar DataAccess List DataAccess Hash Data 14. Perl ABCData AccessList -- access individully#+begin_srcperl @a=qw(fredbarneybettywilma); @b=("tom",[@a],"jerry");#+end_src#+begin_srcperl $c=@a;#$c=4; $c=$b[0];#$c=tom#+end_src 15. Perl ABCData AccessList -- slicing access#+begin_srcperl @a=qw(fredbarneybettywilma); @b=("tom",[@a],"jerry");#+end_src#+begin_srcperl @c=@a;#listcopy ($aa,$ab,$ac@c)=@a;#@c=qw(wilma); @c=@a[1,2,3]; #@c=qw(barneybettywilma); @c=@a[1..3];#samething @a[1,2]=@a[2,1];#switchvalue#+end_src 16. Perl ABC Data AccessList access as a wholeforeachmapgrep 17. Perl ABC Data AccessList access as a wholeforeach#+begin_srcperl@a=(3,5,7,9);foreach$one(@a){$one*=3;}#@aisnow(9,15,21,27)[email protected],notabug. 18. Perl ABC Data AccessList access as a wholemap@a=(3,5,7,9);@b=map{$_*3}@a;#@bisnow(9,15,21,27)@c=map{$_>5}@a;#@cisnow(,1,1)grep@a=(3,5,7,9);@c=grep{$_>5}@a;#@cisnow(7,9)@c=grep{$_>5?$_:()}@a;#equivalentasmap 19. Perl ABC Data AccessList access as a wholemap and equivalent foreach@a=(3,5,7,9);@b=map{$_*3}@a;#@bisnow(9,15,21,27)#equivalentforeachforeachmy$a(@a){push@b,$a*3;#didnotreturnvalues} 20. Perl ABCData AccessList access as a wholesubtime3{map and equivalent foreachmy$num=shift;@a=(3,5,7,9);return$num*3 }@b=map{$_*3}@a; $func=sub{ my$num=shift; return$num*3 }#equivalentssubmy_map{@b=map&time3($_)@a; my($func,$data)=@_;@b=map&$func($_)@a; foreach$a(@$data){@b=my_map&time3,@a; push@b,&$func($a);@b=my_map$func,@a; }return@b;} 21. Perl ABC Data AccessHash -- access individully#+begin_srcperl%words=(fred=>"camel",barney=>"llama",betty=>"alpaca",wilma=>"alpaca",);#+end_src#+begin_srcperl$c=$words{"fred"};#$c=camel$d="barney";$e=$words{$d};#$e=llama#+end_src 22. Perl ABCData AccessHash -- access as a whole#+begin_srcperl #+begin_srcperl%words=(@key_list=keys(%words);fred=>"camel",@value_list=values(%words);barney=>"llama",#+end_srcbetty=>"alpaca",wilma=>"alpaca", #+begin_srcperl);foreach$key(keys(%words){#+end_src print$words{$key},"n";}#+end_src#+begin_srcperlforeach$value(values(%words){print$value,"n";}#+end_src 23. Perl ABC Data AccessList access nested elements#+begin_srcperl@a=qw(fredbarneybettywilma);@b=("tom",[@a],"jerry");@b=("tom",@a,"jerry");#+end_src#+begin_srcperl$a=$b[1];#$a=[@a]$c=$b[1]>[1];#$c=barney$c=@b[1][1];#samething$c=@$a>[1];#samething$c=${$a}[1];#samething#+end_src 24. Perl ABC Data AccessHash access nested elements%h_nest=(fred=>"camel",barney=>"llama",betty=>"alpaca",wilma=>"alpaca",jetsons=>{husband=>"george",wife=>"jane","hisboy"=>"elroy",},);$c=$h_nest{"jetsons"}{"wife"};#$c=jane$j="jetsons";$w="wife";$c=$h_nest{$j}{$w};#samething$jet=$h_nest("jetsons"};#$jethasahash$d=$jet{"husband"};#$d=george 25. Perl ABCData AccessReference#Createsomevariables$a="mamamia";@array=(10,20);%hash=("laurel"=>"hardy","nick"=>"nora");#Nowcreatereferencestothem$r_a=$a;#$ranow"refers"to(pointsto)$a$r_array=@array;$r_hash=%hash; 26. Perl ABC Data AccessAccess Reference Data#Nowcreatereferencestothem$r_a=$a;#$ranow"refers"to(pointsto)$a$r_array=@array;$r_hash=%hash;#Nowaccessthereferenceddata$r_a;#theaddress$$r_a;#the$a;"mamamia";$r_array#theaddress@$r_array#thearray(10,20);@$r_array[1]#theelement20; 27. Perl ABC Data AccessAccess Reference Data#Nowcreatereferencestothem$r_a=$a;#$ranow"refers"to(pointsto)$a$r_array=@array;$r_hash=%hash;#Nowaccessthereferenceddata$r_a;#theaddress$$r_a;#the$a;"mamamia";$r_array#theaddress@$r_array#thearray(10,20);@$r_array[1]#theelement20;