Difference between revisions of "Python-Perl cheatsheet"
Devicerandom (Talk | contribs) |
Devicerandom (Talk | contribs) |
||
Line 29: | Line 29: | ||
| | | | ||
| if x == y | | if x == y | ||
− | | if ( x = y ) | + | | if ( $x == $y ) ''or'' if ( $x eq $y) |
− | | Perl requires parenthesis in a comparison | + | | Perl requires parenthesis in a comparison. Perl uses ''different'' comparators for numbers and strings |
|- | |- | ||
|colspan=4|<span style="color:#009000">'''Arrays'''<span style="color:#009000"> | |colspan=4|<span style="color:#009000">'''Arrays'''<span style="color:#009000"> |
Revision as of 11:14, 26 April 2011
See also the Perl Phrasebook on wiki.python.org
what | Python | Perl | notes |
---|---|---|---|
null type | None | undef | not exactly identical |
String concatenation | "py"+"thon" | "pe"."rl" | |
if x == y | if ( $x == $y ) or if ( $x eq $y) | Perl requires parenthesis in a comparison. Perl uses different comparators for numbers and strings | |
Arrays | |||
declare a list | [1,2,3] | (1,2,3) | note that arrays contain only scalars! (1,2, (3,4), 5) becomes (1,2,3,4,5) |
array length | len(array) | $#array + 1 | Python returns the length of array. Perl gives me the index of the last element. Also, the array value in a scalar context is (usually) its size. |
range(1,100) | (1..100) | ||
create a list from whitespaced values | "fred barney wilma dino".split() | qw( fred barney wilma dino); | You can use separators others than parenthesis, e.g. qw ! fred barney wilma dino ! |
multiple assignment | fred, barney, dino = "flintstone", "rubble", None | ($fred, $barney, $dino) = ("flintstone", "rubble", undef); | |
array concatenation | a+b | (@a, @b) | you can also mix like (@a, 3, @b, undef) |
array.append(item) | push @array, item; | See also Perl's unshift() for attaching to the front of array | |
val = array.pop() | val = pop @array; | See also Perl's shift() for removing from the front of array | |
List iteration | for item in array | foreach $item (@array) | Note that $item is the real element of @array, so if you modify $item, you modify the array content |
list.reverse() | @list = reverse @list; | Python is in place, Perl is not. Same for sort. |