This shows you the differences between the selected revision and the current version of the page.
| notepad:system:perloneliner 2008/03/12 02:06 | notepad:system:perloneliner 2008/03/14 02:00 current | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Perl One-Liners ====== | + | ====== Perl One-Liners and other tricks ====== |
| - | ===== Removing consecutive, duplicated characters ===== | + | ===== Indenting here documents ===== |
| + | ($blah = <<' ENDHEREDOC') =~ s/^ {8}//gm; | ||
| + | blah blah blah blah | ||
| + | blah blah blah blah blah blah | ||
| + | blah blah blah blah blah | ||
| + | ENDHEREDOC | ||
| + | also, it is better to quote the here document tags, so that you can have a semicolon: | ||
| + | $blah = <<'ENDHEREDOC'; | ||
| + | blah blah blah | ||
| + | ENDHEREDOC | ||
| + | ====== Get a piece of text from a matching ====== | ||
| + | In other words, get something from a pattern without using substitution: | ||
| + | ($piece = $whole) =~ (/regex(piece)/)[0]; | ||
| + | |||
| + | ===== Removing consecutive, duplicated characters ===== | ||
| That is, to make ''heello'' to become ''helo'': | That is, to make ''heello'' to become ''helo'': | ||
| s/(.)\1/\1/g; | s/(.)\1/\1/g; | ||
| Line 9: | Line 23: | ||
| The ''s'' line is trival. The ''tr'' line means to have everything (the complement ''c'' of nothing ''<nowiki>//</nowiki>'') and its consecutives to replace by itself. | The ''s'' line is trival. The ''tr'' line means to have everything (the complement ''c'' of nothing ''<nowiki>//</nowiki>'') and its consecutives to replace by itself. | ||
| + | ===== Converting every word into first-letter uppercase ===== | ||
| + | Such As This One | ||
| + | s/(\w+)/\u\L$1/g; | ||
| + | The ''\u'' and ''\L'' are modifiers to make the next character(s) to be upper or lower cases. | ||
| + | |||
| + | ===== Capturing nested parenthesis ===== | ||
| + | Such as capturing the while function call to ''foo()'' in ''foo(a+(b+c)+(d+(e+f)))+g+(h+i)''. The following piece of code is from "Mastering Regular Expression" by Friedl. | ||
| + | my $string = "foo(a+(b+c)+(d+(e+f)))+g+(h+i)"; | ||
| + | my $nest; | ||
| + | $nest = qr/\( ( [^()] | (??{$nest}) )* \)/x; | ||
| + | $string =~ m/(foo$nest)/; | ||
| + | print "Function call is $1\n"; | ||
| + | The ''(??{code})'' construct is used to embed a piece of code into a regex, which a nesting is done. | ||
| + | |||
| + | ===== Separating CSV data ===== | ||
| + | Where the CSV data may include commas inside a pair of quote and quotes may be escaped inside quotes. The following piece of code is from "Mastering Regular Expression" by Friedl. | ||
| + | @new = (); | ||
| + | push(@new, $+) while $text =~ m{ | ||
| + | "([^\"\\]*(?:\\.[^\"\\]*)*)",? # groups the phrase inside the quotes | ||
| + | | ([^,]+),? | ||
| + | | , | ||
| + | }gx; | ||
| + | push(@new, undef) if substr($text,-1,1) eq ','; | ||
| + | |||
| + | ===== Schwartzian Transform ===== | ||
| + | A way to speed up the sorting process by Randal L Schwartz: | ||
| + | @sorted = map { $_->[0] } | ||
| + | sort { $a->[1] cmp $b->[1] } | ||
| + | map { [ $_, foo($_) ] } @data; | ||
| + | The above is to get a bunch of items from ''@data'', and construct a temporary 2-column array for sorting (i.e. calling ''foo()'' for N times only). Then sort the 2-column array according to the string order of the 2nd column, and then extract the first column as the sorted result. | ||
| + | ===== Opening a really temporary file ===== | ||
| + | Passing ''undef'' as filename: | ||
| + | open my $tmp, '+>', undef or die $!; | ||
| + | or you can open a temporary file at RAM if you're using Perl 5.8+: | ||
| + | open(my $wr, '>', \$string) or die "Couldn't write to string"; | ||
| + | open(my $rd, '<', \$string) or die "Couldn't read string"; | ||