in reply to Extracting and manipulating a range of lines

If you just want to print the table you don't need to store anything. If you need to manipulate the data (including using string lengths of the data to format the table nicely) then use an array of hashes.

In any case, a neat way to manage the file is to use the first field name as the line end string so you can read an entire record at a hit:

#!/usr/bin/perl use warnings; use strict; use 5.010; my $startField = "Some Field :"; my $format = "%-15s %8s %8s %6s\n"; printf $format, 'something', 'number1', 'number2', 'word'; local $/ = $startField; while (defined (my $rec = <DATA>)) { next if $rec eq $startField; # Skip empty first record my @fields = split "\n", $rec; my @values; $fields[0] = $startField . $fields[0]; # Restore field label for my $field (@fields[0 .. 3]) { my (undef, $value) = split ': ', $field, 2; push @values, $value; } printf $format, @values; } __DATA__ Some Field : some value Another Field: 1234 Different One: 5678 Yet Another : foo line of uninteresting stuff Some Field : some else Another Field: 4321 Different One: 8765 Yet Another : bar more junk Some Field : another value Another Field: 1122 Different One: 5566 Yet Another : baz

Prints:

something number1 number2 word some value 1234 5678 foo some else 4321 8765 bar another value 1122 5566 baz

Note the local $/ = $startField; line.

True laziness is hard work

Replies are listed 'Best First'.
Re^2: Extracting and manipulating a range of lines
by marinersk (Priest) on Oct 19, 2013 at 07:50 UTC
    Elegant. Simply elegant.