Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
One-liner program
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Perl=== Here are examples in the [[Perl]] [[programming language]]: * Look for duplicate words perl -0777 -ne '<syntaxhighlight lang="perl" inline>print "$.: doubled $_\n" while /\b(\w+)\b\s+\b\1\b/gi</syntaxhighlight>' * Find Palindromes in /usr/dict/words perl -lne '<syntaxhighlight lang="perl" inline>print if $_ eq reverse</syntaxhighlight>' /usr/dict/words * in-place edit of *.c files changing all foo to bar perl -p -i.bak -e '<syntaxhighlight lang="perl" inline>s/\bfoo\b/bar/g</syntaxhighlight>' *.c Many one-liners are practical. For example, the following [[Perl]] one-liner will reverse all the bytes in a file: <syntaxhighlight lang="bash"> perl -0777e 'print scalar reverse <>' filename </syntaxhighlight> While most [[Perl]] one-liners are imperative, Perl's support for anonymous functions, closures, map, filter ([[grep]]) and fold (List::Util::reduce) allows the creation of 'functional' one-liners. This one-liner creates a function that can be used to return a list of primes up to the value of the first parameter: <syntaxhighlight lang="perl"> my $z = sub { grep { $a=$_; !grep { !($a % $_) } (2..$_-1)} (2..$_[0]) } </syntaxhighlight> It can be used on the command line, like this: perl -e'<syntaxhighlight lang="bash" inline>$,=",";print sub { grep { $a=$_; !grep { !($a % $_) } (2..$_-1)} (2..$_[0]) }->(shift)</syntaxhighlight>' number to print out a comma-separated list of primes in the range 2 - number.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)