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

thanks for the starter point. I tried the code but it doesn't seem to be doing what I was hoping. Basically, the desired output data will be comprised of lines that have columns of data for each satellite (e.g.: SATID). The data for each satellite is contained between "SATID ... ... SIGNAL XX". The data for each new satellite will have a completely new set of rows assigned to it.When a SATID is repeated on the line below, it would be located immediately below the matching SATID found in the previous line
In this way, I'm trying to keep the data for each satellite organized. Hope this makes sense =)

Replies are listed 'Best First'.
Re^3: Sorting Data?
by tybalt89 (Monsignor) on Mar 31, 2017 at 03:12 UTC

    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
      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:  <%-{-{-{-<