Output:#!/usr/bin/perl use strict; use warnings; use Data::Dumper; # hash for data my $data = {}; # array of column names my @cols = (); for (<DATA>) { # normaliize the data next if /^\s/ or /^Annular/; if (/^Site/) { # grab column titles, minus the key (Site) @cols = map {$_} split /\s+/; shift @cols; } elsif (/\S+/) { # parse column data my @line = / ([A-Za-z ]+)\s+ # Site (\S+\s+\S+)\s+ # Longitude (\S+\s+\S+)\s+ # Latitude (\S+)\s+ # Elvn (\S+\s+\S+\s+\S+)\s+ # U.T. (\S+)\s+ # PA (\S+) # Alt /x; # clean the key my $site = do { $_ = shift @line; s/\s+$//; $_ }; next unless $site; # map titles to data my %line = do { @_{@cols} = @line; %_}; # add to hash $data->{$site} = \%line; } } # tada print Dumper $data; __DATA__ Annular-Total Eclipse of 2023 Apr 20 - multisite predictions 1st Contact Site Longitude Latitude Elvn U.T. PA Alt o ' o ' m h m s o o Auckland 174 45. -36 55. 0 4 33 59 313 13 Blenheim 173 55. -41 35. 30 4 40 34 326 11 Cape Palliser 175 25. -41 35. 0 4 42 28 327 9 Cape Reinga 172 45. -34 25. 50 4 30 11 307 17 Carterton 175 35. -41 5. 0 4 40 35 324 10 Dannevirke 176 5. -40 15. 200 4 39 9 321 10 East Cape 178 35. -37 45. 0 4 37 58 315 10 Featherston 175 25. -41 5. 40 4 40 36 325 10 Gisborne 178 5. -38 45. 0 4 38 29 317 10 Great Barrier Is 175 25. -36 15. 0 4 34 15 312 13
$VAR1 = {
'Auckland' => {
'Longitude' => '174 45.',
'Alt' => '13',
'Elvn' => '0',
'Latitude' => '-36 55.',
'PA' => '313',
'U.T.' => '4 33 59'
},
In reply to Re: Module for parsing tables from plain text document
by Anonymous Monk
in thread Module for parsing tables from plain text document
by GrandFather
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |