in reply to Re^2: Is there a "Here Table"?
in thread Is there a "Here Table"?
My quick whack at it:
sub parse_HERE_table($) { my @lines = split /[\r\n]+/, $_[0]; my $pattern = splice @lines, 1, 1; my $len = length $pattern; $pattern =~ y/-/A/; # to use \b, we need \w chars, and '-' is not +\w. $pattern =~ s/\bA/(A/g; # too bad we can't do s/\</(/g and s/\>/)/ +g :-( $pattern =~ s/A\b/A)/g; $pattern =~ y/A/./; ( my $header, @lines ) = map { [ map { s/\s+$//; $_ } /$pattern/ ] } # parse; trim trai +ling whitespace from each value. map { $_.(' 'x($len-length($_))) } # pad with spaces to ensure + it's long enough to match. grep { /\S/ } # skip blank lines. @lines; [ map { my %r; @r{ @$header } = @$_; \%r } @lines ] } my $arrayref = parse_HERE_table <<EOF; Name UPP Age Career Terms -------- ------ --- ---------- ----- Rejnaldi 765987 38 Citizen 6 Lisandra 6779AA 34 Noble 4 Kuran 899786 42 Marine 8 EOF
This code assumes well-formed input. You could certainly add error checking and so on.
|
|---|