Created
February 9, 2019 20:26
-
-
Save leszekdubiel/46aa24388b067bc31c2e758ace6f3f51 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| printf "\tname John\tage 24\t\ncity Kraków\tname Ann\tabout works here\tage 45\n\tspeaks Polish\tname Leszek\t\r\v\b\t\tnote malformed line still works\n" | perl -e 'use utf8; use Modern::Perl qw{2017}; use Data::Dumper; no warnings qw{uninitialized}; while (<STDIN>) { my %h = map { split / /, $_, 2 } grep { / / } split /[^[:print:]]/, $_, -1; 0 && warn Dumper(\%h); print "\t$_ $h{$_}" for @ARGV ? @ARGV : sort keys %h; print "\t\n"; }' name age about |
Author
O! Thank you... I think I didn't get email that you have commented. I will test that too.
:)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the input to the one-liner:
First let's turn the Perl one-liner into something easier to understand
The
split /[^[:print:]]/, $_, -1produces something like:The
grep { / / }cleans it up toThat combination directly translates to:
The thing is
.splitcan take a:skip-emptynamed parameter:But we can do better than that with
.comb:Next up is
map { split / /, $_, 2 }.It takes the above strings and splits them at the first space.
That creates a list of lists, so we need to add a
.flatThis is what we have so far:
We have to add the part where it takes an argument or sorts by key, and prints it:
That complains about undefined values in the
printstatement.We don't care about that so we tell it to be quiet with
quietlyNow to combine it into a one-liner, and remove other extra characters.
perl6 -e 'for $*IN.lines {my %h=.comb(/<print>+/)».split(" ",2).flat;quietly print "\t$_ %h{$_}" for @*ARGS||%h.keys.sort;print "\t\n"}'