Perl Notes

From Moo
Jump to: navigation, search

find and capture in one line[edit]

my ($found) = ($text =~ /pre (capture stuff in brackets) post/)

Note the brackets around $found - match list is converted to a number for a scalar

sort[edit]

@sorted = sort @array;
Numerical:
@sorted = sort { $a <=> $b } @notsorted;
Alphabetical sort:
@sorted = sort { lc($a) cmp lc($b) } @notsorted;
Alphabetical sort, reversed:
@sorted = sort { lc($b) cmp lc($a) } @notsorted;

join log lines so we can grep[edit]

e.g. log4j where each line starts with 2014-12-01 but stack traces/multi line buffers don't

perl -ne '$_ =~ s/\n//; if ($_ =~ /^\d{4}-\d{2}-\d{2}/) { print "\n$_"; } else { print "|$_"; }'