in reply to Arrays & Output printing?

First, a quick oneliner for fun:

perl -pe 'chomp if s/^START//..s/^END//; s/\S\K$/ /' input.txt

However, so far you haven't gotten an answer that sorts your fields the way you showed in your desired output, so I'll try my hand at that:

use warnings; use strict; my @ORDER = qw/ TIME ALT TEMP COLOR STRENGTH /; my %ORDER = map {$ORDER[$_]=>$_+1} 0..$#ORDER; local $/ = "\nEND\n"; while (<>) { s/\A.*^START\n(.*)^END\n/$1/ms; next unless /\S/; my @lines = grep {/\S/} split /\n/; @lines = map { $$_[0] } sort { return $$a[1] <=> $$b[1] if $$a[1] && $$b[1]; return -1 if $$a[1]; return 1 if $$b[1]; return $$a[2] cmp $$b[2] } map { /^(\w+):/ ? [$_,$ORDER{$1},$1] : [$_,0,$_] } @lines; print join(" ", @lines), "\n"; }

See Schwartzian transform and How do I sort an array by (anything)? for the sorting technique I used.

Replies are listed 'Best First'.
Re^2: Arrays & Output printing?
by tybalt89 (Monsignor) on Mar 27, 2017 at 15:24 UTC

    I wondered about the output order also, but figured it was just a typo. However if the specified order is actually wanted:

    #!/usr/bin/perl -l # http://perlmonks.org/?node_id=1186032 use strict; use warnings; $/ = "END\n"; my @ORDER = qw/ TIME ALT TEMP COLOR STRENGTH /; for my $section (<DATA>) { print join ' ', map $section =~ /\b($_: \S+)/, @ORDER; } __DATA__ START TIME: 3 ALT: 3.1 TEMP: 4.3 END START TIME: 2.6 ALT: 8 TEMP: 1 END START TIME: 6.1 ALT: 7.2 COLOR: 43 STRENGTH: 7 TEMP: 9.3 END
Re^2: Arrays & Output printing?
by tybalt89 (Monsignor) on Mar 27, 2017 at 16:11 UTC

    Another version of a one-liner for fun :)

    perl -lne 'INIT{$/="END";$,=" "}print /\w+:.*/g' input.txt