Perl Cheat Sheet Functions Get information on a function by typing, e.g., perldoc -f chomp at the command line. Scalar variables while (defined ($x=<>)) {code} False if variable has never been set (or when you try to read past the end of an input file) length($x) Length of a string chomp($line); Remove a newline at the end of a string $short=substr ($long, 2, 5) Characters 3-7 of $long (first char is 0!) Arrays push @arr, $x Add to end of array $x = pop @arr; Remove last element of array, put in $x shift @arr; (See also unshift) Remove first element of an arrray, put in $x $size = scalar @arr; Number of things in the array See also: split, join, splice, sort split string->array, join array->string, delete part of array, sort array in many ways Hashes @key = keys %hash The lookup terms in the hash if (exists $hh{“Hsp”}) {...} See whether hash %hh has a value for key Hsp Input/Output and Files open(HANDLE, ">outfile") or die “Can’t open $outfile: $!\n” Open outfile for writing, and associate it with filehandle HANDLE. Use “ Read a line from filehandle HANDLE, put in $x close(HANDLE); Stop reading/writing a previously opened file Exit exit; Exits the program die "Something broke!\n"; Exits the program with error message Operators and Loops Assign value $x = 1 Sets variable to a value. Don’t confuse with ==, which tests whether numerical values are equal Math print 1 * (2 + 3/4) Regular math symbols 10%3==1; 12%3==0 Modulus (remainder) operator $x += 4; Same as $x=$x+4; Also -= *= /= $x++; Same as $x=$x+1; Conditions if (.1 == 0.1) {print “same num”} Are numbers equal? Don’t confuse with = or eq if (1 != 2) {print “diff num”} Are numbers different? > < >= <= Are numbers greater than, less than, etc. if (“a” eq “a”) {print “same text”} Does text have exactly the same letters? if (“A” ne “a”) {print “diff text”} Does text have different letters? if (($x > 1) && ($x < 2)) {code} Logical AND (true if both sides are true) if (($x > 10) || ($x < -10)) {code} Logical OR (true if one or both sides are true) =~ !~ Test for a match: See Matching cheat sheet Loops foreach my $i (1 .. 100) {code} (for and foreach are equivalent) Sets $i to 1 and does code. Sets $i to 2, … up to (and including) 100 while ($a < $b) {code} Does code while the condition is true (If condition is false, never enters the loop.) Matching and Regular Expressions Test for Match =~ Test for match if ($x =~ /abc/) { ...} Does $x have the string “abc” anywhere in it? !~ Test for non-match if ($x !~ /abc/) { ...} Does $x NOT have the string “abc” anywhere in it? $_ Default variable if (/abcd/) { s/bc/x/ } // and s/// work on $_ by default, no =~ needed Substitute s/// Do a Substitution $x =~ s/abc/def/; Replace (only) first occurrence of “abc” in $x with def Match/ Sub Options i Ignore case. /abc/i Matches abc, ABC, aBc, etc. g Global substitution. s/a/c/g Replace ALL occurrences Special Match Items . Any one character (except \n) /a.c/ “arc”, “a9c”, but not “ac”. [ ] Any one of. /[abc]/ Any one of “a”, “b”, or “c”. [a- zA-Z] matches any letter \d Digit (Same as [0-9]) /\d\d:\d\d/ “10:30” (but not “9:30”) \s Space, tab, or newline /^\s*$/ An empty line. \ Literally match special characters: + * ( ) / [ ] \ | { } ^ $ @ /1\+2/ “1+2”, not “1112”. The backslash “quotes” or “escapes” the plus sign. Item Locations ^ Beginning of a line /^a/ "arginine" but not "valine”. $ End of a line /a$/ "beta" but not "beat". Item Repetitions ? An optional thing /ab?c/ “ac” or “abc”. * Any number of copies OR nothing at all /a*/ "", "a", "aaa". + Any number of copies /a+b/ "ab" or "aaab" but not "b". { } m to n copies /ab{2,4}c/ “abbc”, “abbbc”, “abbbbc”, but not “abc” or “abbbbbc” Misc | One or the other /abc|def/ “abc” or “def” ( ) Capture parts of match in numbered variables /a(b(..)e)f/ “abcdef”. This will also set $1 to “bcde” and $2 to “cd”. AND group things together for repetition, etc. /a(bc)+d/ “abcd” or “abcbcbcbcd”




需要 2 金币 [ 分享pdf获得金币 ] 1 人已下载





下载需要 2 金币 [金币充值 ]
亲,您也可以通过 分享原创pdf 来获得金币奖励!