in reply to Re^2: Sorting Data?
in thread Sorting Data?

Like this - each SATID has its own column in sorted order.

#!/usr/bin/perl -l # http://perlmonks.org/?node_id=1186568 use strict; use warnings; my @secondpass; my %keys; while(<DATA>) { push @secondpass, $_; $keys{$_}++ for /SATID (\d\d)/g; } my @keyorder = sort keys %keys; for (@secondpass) { my %items = reverse /(SATID (\d\d).{27})/g; print join ' ', map { $items{$_} // ' ' x 35 } @keyorder; } __DATA__ SATID 02 VAL1 32 VAL2 275 SIGNAL 43 SATID 24 VAL1 10 VAL2 098 SIGNAL 1 +2 SATID 41 VAL1 87 VAL2 180 SIGNAL 15 SATID 24 VAL1 41 VAL2 103 SIGNAL 56 SATID 02 VAL1 41 VAL2 154 SIGNAL 3 +1 SATID 41 VAL1 93 VAL2 124 SIGNAL 21 SATID 41 VAL1 23 VAL2 132 SIGNAL 23 SATID 24 VAL1 32 VAL2 034 SIGNAL 3 +2 SATID 02 VAL1 23 VAL2 145 SIGNAL 31 SATID 41 VAL1 63 VAL2 305 SIGNAL 62 SATID 33 VAL1 31 VAL2 174 SIGNAL 4 +4 SATID 02 VAL1 45 VAL2 205 SIGNAL 34

Replies are listed 'Best First'.
Re^4: Sorting Data?
by coding1227 (Novice) on Mar 31, 2017 at 03:21 UTC
    YES! This is perfect! =) Thank you so much. Can you explain (briefly) how the code works? I'd like to understand a little bit better what "magic" it is doing..
      Can you explain ...? I'd like to understand a little bit better ...

      You can gain insight yourself into the operation of this (or any) program by inserting print/dump statements at strategic points to see how data is extracted and transformed and how structures are built up:

      use Data::Dumper; ... my %keys; while(<DATA>) { push @secondpass, $_; $keys{$_}++ for /SATID (\d\d)/g; } print Dumper \%keys; my @keyorder = sort keys %keys; print Dumper \@keyorder; for (@secondpass) { my %items = reverse /(SATID (\d\d).{27})/g; print Dumper \%items; <STDIN>; print join ' ', map { $items{$_} // ' ' x 35 } @keyorder; } ...
      The  <STDIN>; after the print statement in
          print Dumper \%items;  <STDIN>;
      in the for-loop just pauses output until you hit Enter so you are not overwhelmed by stuff. (NB: My personal favorite structured data dumper is Data::Dump, but Data::Dumper is core. :)


      Give a man a fish:  <%-{-{-{-<