in reply to Is there a "Here Table"?
It's hard to see the need for a module to do that. Especially as the '----' line is very specific to your data format; thus would need a special case or option.
And because it is very simple to do:
#! perl -slw use strict; use Data::Dump qw[ pp ]; my @keys = split ' ', scalar <DATA>; <DATA>; ## discard ----- my @data = map{ my %hash; @hash{ @keys } = split ' '; \%hash; } <DATA>; pp \@data; __END__ Name UPP Age Career Terms -------- ------ --- ---------- ----- Rejnaldi 765987 38 Citizen 6 Lisandra 6779AA 34 Noble 4 Kuran 899786 42 Marine 8
Produces:
C:\test>junk [ { Age => 38, Career => "Citizen", Name => "Rejnaldi", Terms => 6, UP +P => 765987 }, { Age => 34, Career => "Noble", Name => "Lisandra", Terms => 4, UPP +=> "6779AA" }, { Age => 42, Career => "Marine", Name => "Kuran", Terms => 8, UPP => + 899786 }, ]
Of course, someone will complain that it doesn't handle names with spaces, and so you need to switch to fixed field record processing:
#! perl -slw use strict; use Data::Dump qw[ pp ]; my @keys = unpack 'A8xA6xA3xA10xA5', scalar <DATA>; <DATA>; ## discard my @data = map{ my %hash; @hash{ @keys } = unpack 'A8xA6xA3xA10xA5', $_; \%hash; } <DATA>; pp \@data; __END__ Name UPP Age Career Terms -------- ------ --- ---------- ----- Rejnaldi 765987 38 Citizen 6 Lisandra 6779AA 34 Noble 4 Kuran 899786 42 Marine 8
Which produces the same output. But ... they'll say: what if you want to read lots of different files in the same format?
Then you need to determine the fields sizes from the data:
#! perl -slw use strict; use Data::Dump qw[ pp ]; my @keys = scalar( <DATA> ) =~ m[(\S+\s*)\s]g; my $templ = join 'x', map{ 'A' . length() } @keys; @keys = map{ $_ =~ s[\s+$][]; $_ } @keys; <DATA>; ## discard my @data = map{ my %hash; @hash{ @keys } = unpack $templ, $_; \%hash; } <DATA>; pp \@data; __END__
Again, same output.
But what if the keys can contain spaces? In which case you'll need to use a heuristic approach to locating the field boundaries
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Is there a "Here Table"?
by jdporter (Paladin) on Apr 07, 2015 at 22:09 UTC | |
by jdporter (Paladin) on Apr 08, 2015 at 02:57 UTC | |
by BrowserUk (Patriarch) on Apr 07, 2015 at 22:31 UTC |