Skip to main content
added 322 characters in body
Source Link
user218374
user218374

Shorter version where we don't assign empty values to zero:

perl -lane '
   ++$h{$i[!$h{$F[0]} ? @i : -1]=$F[0]}{$F[1]}}{
   print join "\t", "\t", @h = sort keys %{ +{ map { map { $_ => 1 } keys %$_ } values %h } };
   print join "\t", $_, @{$h{$_}}{@h} for @i;
' yourfile

perl -lane '
   $i[@i]=$F[0] unless $h{$F[0]};
   ++$h{$F[0]}{$F[1]}}{
   @h = sort keys %{ +{ map { map { $_ => 1 } keys %$_ } values %h } };
   print join "\t", "\t", @h;
   for my $date ( @i ) {
      my $href = $h{$date};
      print join "\t", $date, map { $href->{$_} || 0 } @h;
   }
' yourfile

Results

                A       B       C
2017-07-30      3       2       1
2017-07-31      1       2       2

Data Structures:

  • hash %h which has keys the dates and values sub-hashes whose keys are A, B, C, etc. and corresponding values are their respective counts on those particular dates.

  %h = (
       2017-07-30 => {
           A => 3,
           B => 2,
           C => 1,
       },
       ...
  );

  • Array @i which stores the dates in the order they were encountered. We push the dates into the array @i only when it's not been seen earlier IOW, when it's seen for the first time only. The order is provided by the array position.
  • Array @h has the uniquified keys after totaling all the "A", "B", "C", etc. keys from the hash %h.
perl -lane '
   $i[@i]=$F[0] unless $h{$F[0]};
   ++$h{$F[0]}{$F[1]}}{
   @h = sort keys %{ +{ map { map { $_ => 1 } keys %$_ } values %h } };
   print join "\t", "\t", @h;
   for my $date ( @i ) {
      my $href = $h{$date};
      print join "\t", $date, map { $href->{$_} || 0 } @h;
   }
' yourfile

Results

                A       B       C
2017-07-30      3       2       1
2017-07-31      1       2       2

Data Structures:

  • hash %h which has keys the dates and values sub-hashes whose keys are A, B, C, etc. and corresponding values are their respective counts on those particular dates.

  %h = (
       2017-07-30 => {
           A => 3,
           B => 2,
           C => 1,
       },
       ...
  );

  • Array @i which stores the dates in the order they were encountered. We push the dates into the array @i only when it's not been seen earlier IOW, when it's seen for the first time only. The order is provided by the array position.
  • Array @h has the uniquified keys after totaling all the "A", "B", "C", etc. keys from the hash %h.

Shorter version where we don't assign empty values to zero:

perl -lane '
   ++$h{$i[!$h{$F[0]} ? @i : -1]=$F[0]}{$F[1]}}{
   print join "\t", "\t", @h = sort keys %{ +{ map { map { $_ => 1 } keys %$_ } values %h } };
   print join "\t", $_, @{$h{$_}}{@h} for @i;
' yourfile

perl -lane '
   $i[@i]=$F[0] unless $h{$F[0]};
   ++$h{$F[0]}{$F[1]}}{
   @h = sort keys %{ +{ map { map { $_ => 1 } keys %$_ } values %h } };
   print join "\t", "\t", @h;
   for my $date ( @i ) {
      my $href = $h{$date};
      print join "\t", $date, map { $href->{$_} || 0 } @h;
   }
' yourfile

Results

                A       B       C
2017-07-30      3       2       1
2017-07-31      1       2       2

Data Structures:

  • hash %h which has keys the dates and values sub-hashes whose keys are A, B, C, etc. and corresponding values are their respective counts on those particular dates.

  %h = (
       2017-07-30 => {
           A => 3,
           B => 2,
           C => 1,
       },
       ...
  );

  • Array @i which stores the dates in the order they were encountered. We push the dates into the array @i only when it's not been seen earlier IOW, when it's seen for the first time only. The order is provided by the array position.
  • Array @h has the uniquified keys after totaling all the "A", "B", "C", etc. keys from the hash %h.
Source Link
user218374
user218374

perl -lane '
   $i[@i]=$F[0] unless $h{$F[0]};
   ++$h{$F[0]}{$F[1]}}{
   @h = sort keys %{ +{ map { map { $_ => 1 } keys %$_ } values %h } };
   print join "\t", "\t", @h;
   for my $date ( @i ) {
      my $href = $h{$date};
      print join "\t", $date, map { $href->{$_} || 0 } @h;
   }
' yourfile

Results

                A       B       C
2017-07-30      3       2       1
2017-07-31      1       2       2

Data Structures:

  • hash %h which has keys the dates and values sub-hashes whose keys are A, B, C, etc. and corresponding values are their respective counts on those particular dates.

  %h = (
       2017-07-30 => {
           A => 3,
           B => 2,
           C => 1,
       },
       ...
  );

  • Array @i which stores the dates in the order they were encountered. We push the dates into the array @i only when it's not been seen earlier IOW, when it's seen for the first time only. The order is provided by the array position.
  • Array @h has the uniquified keys after totaling all the "A", "B", "C", etc. keys from the hash %h.