Perl Cheatsheet
50 essential methods for coding interviews
Showing 50 of 50 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
push | push(@array, $value) | Append one or more elements to the end of an array | O(1) amortized | essential |
pop | pop(@array) | Remove and return the last element of an array | O(1) | essential |
shift | shift(@array) | Remove and return the first element of an array | O(n) | essential |
unshift | unshift(@array, $value) | Prepend one or more elements to the beginning of an array | O(n) | essential |
splice | splice(@array, $offset, $length, @list) | Remove and/or add elements at any position in an array | O(n) | essential |
scalar | scalar(@array) | Return the number of elements in an array | O(1) | essential |
join | join($separator, @array) | Concatenate array elements into a string with separator | O(n) | essential |
split | split(/pattern/, $string, $limit) | Split a string into an array based on a pattern | O(n) | essential |
sort | sort { $a <=> $b } @array | Return sorted list (default: lexicographic order) | O(n log n) | essential |
grep | grep { condition } @array | Filter array elements based on a condition (like filter) | O(n) | essential |
map | map { expression } @array | Transform each element of an array | O(n) | essential |
keys | keys(%hash) | Return a list of all keys in a hash | O(n) | essential |
values | values(%hash) | Return a list of all values in a hash | O(n) | essential |
exists | exists($hash{$key}) | Check if a key exists in a hash | O(1) | essential |
delete | delete($hash{$key}) | Remove a key-value pair from a hash | O(1) | essential |
length | length($string) | Return the number of characters in a string | O(1) | essential |
substr | substr($string, $offset, $length, $replacement) | Extract or replace a substring | O(n) | essential |
index | index($string, $substring, $position) | Find position of substring (returns -1 if not found) | O(n*m) | essential |
chomp | chomp($string) | Remove trailing newline from string (modifies in place) | O(1) | essential |
m// (match) | $string =~ m/pattern/flags | Test if pattern matches string | O(n) to O(n*m) | essential |
s/// (substitute) | $string =~ s/pattern/replacement/flags | Search and replace pattern in string | O(n) | essential |
captures ($1, $2) | if ($str =~ /(w+)/) { print $1; } | Access captured groups from regex match | O(n) | essential |
int | int($number) | Truncate to integer (toward zero) | O(1) | essential |
max/min | use List::Util qw(max min);
max(@array) | Return maximum or minimum value from a list | O(n) | essential |
sum | use List::Util qw(sum);
sum(@array) | Return sum of all elements | O(n) | essential |
sort (custom) | sort { $a->{key} <=> $b->{key} } @array | Sort array of hashes or complex structures | O(n log n) | essential |
sort (descending) | sort { $b <=> $a } @array | Sort array in descending order | O(n log n) | essential |
\ (reference) | my $ref = \$scalar; my $aref = \@array | Create a reference to a variable | O(1) | essential |
-> (dereference) | $aref->[0], $href->{key} | Access elements through a reference | O(1) | essential |
defined | defined($variable) | Check if a variable has a defined value (not undef) | O(1) | essential |
reverse | reverse(@array) | Return elements in reverse order (does not modify original) | O(n) | common |
reduce | use List::Util qw(reduce);
reduce { $a + $b } @array | Reduce array to single value by applying function cumulatively | O(n) | common |
first | use List::Util qw(first);
first { condition } @array | Return the first element matching a condition | O(n) | common |
any | use List::Util qw(any);
any { condition } @array | Return true if any element matches the condition | O(n) | common |
all | use List::Util qw(all);
all { condition } @array | Return true if all elements match the condition | O(n) | common |
each | each(%hash) | Return next key-value pair as a list | O(1) per call | common |
%hash = () | %hash = () | Clear all entries from a hash | O(n) | common |
rindex | rindex($string, $substring, $position) | Find last position of substring | O(n*m) | common |
uc | uc($string) | Convert string to uppercase | O(n) | common |
lc | lc($string) | Convert string to lowercase | O(n) | common |
sprintf | sprintf($format, @values) | Return formatted string (C-style formatting) | O(n) | common |
tr/// (transliterate) | $string =~ tr/search/replace/ | Character-by-character translation | O(n) | common |
qw// | qw/word1 word2 word3/ | Quote words - create list from whitespace-separated words | O(n) | common |
abs | abs($number) | Return absolute value | O(1) | common |
ref | ref($variable) | Return the type of reference (ARRAY, HASH, SCALAR, etc.) | O(1) | common |
uniq | use List::Util qw(uniq);
uniq(@array) | Remove duplicate elements preserving order | O(n) | common |
chop | chop($string) | Remove and return last character (modifies in place) | O(1) | useful |
sqrt | sqrt($number) | Return square root | O(1) | useful |
rand | rand($max) | Return random float between 0 and max (default 1) | O(1) | useful |
shuffle | use List::Util qw(shuffle);
shuffle(@array) | Randomly reorder array elements | O(n) | useful |