coding1227 has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks,

I'm trying to read in a 2-column file and print the contents in an organized manner. I tried using an array (see attached code) but I'm having some trouble making it work correctly. Any help with this would be incredibly appreciated! =)


Input 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


Desired output:
TIME: 3 ALT: 3.1 TEMP: 4.3
TIME: 2.6 ALT: 8 TEMP: 1
TIME: 6.1 ALT: 7.2 TEMP: 9.3 COLOR: 43 STRENGTH: 7


#!/usr/bin/perl #perl -e 'while (<>){print if (/^START/../^END/);}' input.txt use strict; my @array; open(my $fh, "<", "../temp/out2.asc") or die "Failed to open file: $!\n"; while(<$fh>) { if (/^START/../^END/){ chomp; push @array, $_; } } close $fh; print join " ", @array; print "\n";

Replies are listed 'Best First'.
Re: Arrays & Output printing?
by huck (Prior) on Mar 27, 2017 at 06:12 UTC

    You got close

    #!/usr/bin/perl use strict; my @array; #open(my $fh, "<", "../temp/out2.asc") # or die "Failed to open file: $!\n"; my $fh=\*DATA; while(<$fh>) { if (/^START/../^END/){ chomp; push @array, $_; } else{ if (scalar(@array)) { print join " ", @array; print "\n"; @array=(); } } } if (scalar(@array)) { print join " ", @array; print "\n"; @array=(); } close $fh; __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
    Result
    D:\goodies\pdhuck\down1\perl\monks>perl 1186032.pl 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
    Because the if .. is true as soon as it first matches and true until after the last match, you got the start and end lines in there too.

    This uses a state flag to do the collection,

    use strict; my @array; #open(my $fh, "<", "../temp/out2.asc") # or die "Failed to open file: $!\n"; my $fh=\*DATA; my $collect=0; while(<$fh>) { if (/^START/) {$collect=1;} elsif(/^END/){ if (scalar(@array)) { print join " ", @array; print "\n"; @array=(); } $collect=0; } elsif($collect){ chomp; push @array, $_; } } close $fh; __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
    result
    D:\goodies\pdhuck\down1\perl\monks>perl 1186032-b.pl TIME: 3 ALT: 3.1 TEMP: 4.3 TIME: 2.6 ALT: 8 TEMP: 1 TIME: 6.1 ALT: 7.2 COLOR: 43 STRENGTH: 7 TEMP: 9.3
    More like what you wanted

    But what if you wanted to save it all till the end of the file loop

    use strict;use warnings; my $array=[]; #open(my $fh, "<", "../temp/out2.asc") # or die "Failed to open file: $!\n"; my $fh=\*DATA; my $collect=0; my $AoA=[]; while(<$fh>) { if (/^START/) {$collect=1;} elsif(/^END/){ if (scalar(@$array)) {push @$AoA,$array;} $array=[]; $collect=0; } elsif($collect){ chomp; push @$array, $_; } } for my $a (@$AoA) { print join(' ',@$a)."\n"; } close $fh; __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
    Result
    D:\goodies\pdhuck\down1\perl\monks>perl 1186032-c.pl TIME: 3 ALT: 3.1 TEMP: 4.3 TIME: 2.6 ALT: 8 TEMP: 1 TIME: 6.1 ALT: 7.2 COLOR: 43 STRENGTH: 7 TEMP: 9.3
    same result as -b but it was saved up into an array o arrays until the end of the input file and printed then.

Re: Arrays & Output printing?
by haukex (Archbishop) on Mar 27, 2017 at 09:19 UTC

    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.

      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

      Another version of a one-liner for fun :)

      perl -lne 'INIT{$/="END";$,=" "}print /\w+:.*/g' input.txt
Re: Arrays & Output printing?
by tybalt89 (Monsignor) on Mar 27, 2017 at 06:42 UTC
    #!/usr/bin/perl # http://perlmonks.org/?node_id=1186032 use strict; use warnings; $/ = "END\n"; while(<DATA>) { chomp; print s/\s+/ /gr =~ s/ ?START //r, "\n" } __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
      FANTASTIC! Thank you SO much everyone for the incredibly helpful examples! I actually am learning lots from each one...

      Thank you thank you thank you!!