c:\@Work\Perl\monks>perl -wMstrict -le
"use warnings;
use strict;
;;
use Regexp::Common;
;;
use Data::Dump qw(dd);
;;
my @lines = (
'Summary
Employee John Doe | -0.82 | ',
'Summary Employee Fred D. Poe | -5.03 | ',
'SummaryEmployee Billy-Bob Toe | | ',
'SummaryEmployee | 999 | ',
'Employee Prince | 123 | ',
'Employee O | 1.23 | ',
);
;;
my $rx_name = qr{ \S+? (?: \s+ \S+)*? }xms;
my $rx_th_open = qr{ \s* < th > \s* }xms;
my $rx_th_close = qr{ \s* < / th > \s* }xms;
;;
my %per_employee;
;;
LINE:
for my $line (@lines) {
my $parsed =
my ($name, $amount) = $line =~ m{
$rx_th_open Employee \s+ ($rx_name) $rx_th_close
$rx_th_open ($RE{num}{real})? $rx_th_close
}xms;
;;
if (not $parsed) {
warn qq{'$line' failed to parse};
next LINE;
}
;;
$amount = 'no amount' unless defined $amount;
$per_employee{$name} = $amount;
}
;;
dd \%per_employee;
"
'SummaryEmployee | 999 | ' failed to parse at -e line 1.
{
"Billy-Bob Toe" => "no amount",
"Fred D. Poe" => "-5.03",
"John Doe" => "-0.82",
O => "1.23",
Prince => 123,
}
---|
---|
---|
---|
---|