in reply to Re: Is there a "Here Table"?
in thread Is there a "Here Table"?

what if the keys can contain spaces?

Nah, just cache the first line and parse it after you've read the second line.

BTW - the data shown is (obviously?) in the format produced by default by sql select statements, at least for some major RDMS... So you'd think that this would be a "solved problem" by now...

I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.

Replies are listed 'Best First'.
Re^3: Is there a "Here Table"?
by jdporter (Paladin) on Apr 08, 2015 at 02:57 UTC

    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.

Re^3: Is there a "Here Table"?
by BrowserUk (Patriarch) on Apr 07, 2015 at 22:31 UTC
    BTW - the data shown is (obviously?) in the format produced by default by sql select statements, at least for some major RDMS... So you'd think that this would be a "solved problem" by now...

    Well, yes. There is Parse::SQLOutput ... but it won't work for the OPs data.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked