Last active
December 16, 2018 05:27
-
-
Save s1037989/de279c3a112d0e62533b8675dd0df0e7 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
use ojo; | |
no warnings; | |
my $j = j(<>); | |
my %c = (); | |
$c{$_}++ foreach map { keys %$_ } @$j; | |
my @c = sort keys %c; | |
say join "\t", @c; | |
say join "\t", @$_{@c} foreach @$j; |
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
package Mojo::ByteStream::Role::Split; | |
use Role::Tiny; | |
sub split { | |
my ($self, $pattern, $limit) = @_; | |
return Mojo::Collection->new(map { $self->new($_) } split $pattern, $$self, $limit//0); | |
} | |
package main; | |
use ojo; | |
use Mojo::Util 'getopt'; | |
no warnings; | |
getopt | |
'1=s' => \my $f1, | |
'2=s' => \my $f2; | |
$f1--; | |
$f2--; | |
die "Usage: $0 tsv1 tsv2\n" unless $#ARGV == 1; | |
my $c1 = b(f($ARGV[0])->slurp)->with_roles('+Split')->split("\n")->map(sub{$_->split("\t", -1)->map('to_string')->to_array}); | |
my $c2 = b(f($ARGV[1])->slurp)->with_roles('+Split')->split("\n")->map(sub{$_->split("\t", -1)->map('to_string')->to_array}); | |
my $h1 = shift @$c1; | |
my $h2 = shift @$c2; | |
say join "\t", @$h1, @$h2; | |
$c1->map(sub{ | |
my $_c1 = $_; | |
if ( my $first = $c2->grep(sub{$_->[$f2] eq $_c1->[$f1]})->first ) { | |
say join "\t", @$_, @$first; | |
} else { | |
say join "\t", @$_; | |
} | |
}); |
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
use ojo; | |
use Mojo::Util 'getopt'; | |
getopt | |
'r|dumper' => \my $dumper, | |
'1|one' => \my $one, | |
'n=i' => \my $n, | |
'd|defined' => \my $defined, | |
'h|head' => \my $head, | |
'c|columns' => \my $columns, | |
'g|grep:s' => \my @grep, | |
'm|musthave=s' => \my @musthave; | |
$ARGV[0] or die "Usage: $0 tsvfile\n"; | |
my @j = (); | |
my $i = 0; | |
my %grep; | |
if ( @grep ) { | |
%grep = map { $_ => 1 } @grep; | |
$grep{$_}++ foreach @musthave; | |
} | |
my @r = split /\n/, f($ARGV[0])->slurp; | |
my $h = shift @r if $head; | |
my @c = split /\t/, $h if $h; | |
say $dumper ? r([@c]) : j([@c]) and exit if $columns; | |
foreach my $r ( @r ) { | |
my @_r = split /\t/, $r; | |
my @_c = @c ? (@c) : (0..$#_r); | |
@{$j[$i]}{@_c} = @_r; | |
if ( $defined ) { | |
foreach ( keys %{$j[$i]} ) { | |
delete $j[$i]{$_} unless $j[$i]{$_} | |
} | |
} | |
if ( %grep ) { | |
foreach ( keys %{$j[$i]} ) { | |
delete $j[$i]{$_} unless $grep{$_}; | |
} | |
} | |
if ( @musthave && $head ) { | |
local @_ = grep { $_ } @{$j[$i]}{@musthave}; | |
$#_ == $#musthave ? $i++ : delete $j[$i]; | |
} else { | |
$i++; | |
} | |
last if $one && $i == 1 or $n && $i == $n; | |
} | |
say $dumper ? r([@j]).$#j : j([@j]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment