Last active
December 16, 2015 21:09
-
-
Save labster/5497694 to your computer and use it in GitHub Desktop.
Oneliners
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program 1 | |
perl6 -n -e '.split(" ")[2].say' 1.txt | |
program 2 | |
perl6 -e 'say [+] $*ARGFILES.lines' 2.txt | |
program 3 | |
perl6 -n -e 'say $_, "\n"' 3.txt | |
program 4 | |
perl6 -e 'my $i; say "{++$i}. $_" for $*IN.lines' < 4.txt | |
program 5 | |
perl6 -e '$*IN.slurp.comb(/<?after \n?START\n> .*? <?before \nEND\n?>/)>>.say' < 5.txtT | |
Though it doesn't like to use $$ and ^^ in <?after>... | |
##Program 6 | |
use v6; | |
my $filename = "3.txt"; | |
say .indent(4) for $filename.IO.lines; | |
# still looks like a one-liner, but it's so easy in Perl 6 | |
# also I'm working on the assumption that you didn't want blank lines with 4 spaces in the original problem, masak. | |
## Program 7 | |
my $filename = "2.txt"; | |
for $filename.IO.lines -> $line { | |
say $line if $line ~~ 20..30; | |
} | |
## Program 8 | |
sub is_palidrome ($text) { | |
$text eq $text.flip; | |
} | |
my $filename = "2.txt"; | |
for $filename.IO.lines { | |
say $text if is_palindrome($text) | |
} | |
#again made an assumption that reverse was used wrong here | |
#otherwise this was kind of a no-op program | |
## Program 9 | |
my $filename = "8.txt"; | |
my $replacements = 0; | |
for $filename.IO.lines -> $line { | |
$line ~~ s:g/<|w>PERL<|w>/Perl/ and $replacements++; | |
$line.say; | |
} | |
print $replacements; | |
## Program 10 | |
my $filename = "10.txt"; | |
my @column-range = 2..4; | |
for $filename.IO.lines -> $line { | |
$line.split[@column-range].join(' : ').say; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment