johnr has asked for the wisdom of the Perl Monks concerning the following question:
I have another one of these subroutines (in another module) that gets html as input and iterates HTML::TableExtract output in the foreach lop.
It seems like there should be a framework or something to do this kind of task. If not that, than some way to be more concise yet still clear in intent. It may be I am am trying to do a lot and this is what it takes.sub get_state { my ($self, $contents) = @_; $contents =~ s/\r//g; my %session = (); foreach (split /\n/, $contents) { if (/^ *\d+\+*\|/) { # Data record my @values = split(/\|/); foreach (@values) { s/^\ *//; }; my %stats; @stats{@{$self->{fields}}} = @values; # do some clean up / bring to common values $stats{position} =~ s/\+//g; # sometimes there is a "+" $stats{driver} =~ s/ *\(R\)$//; # I don't care if rookie $stats{best_speed} =~ s/\+//g; # sometimes there is a "+" at +end if ($stats{status} eq '') { # not all the series track run, pi +t, etc $stats{status} = 'Run' } $stats{status} =~ s/In Pit/Pit/ig; $stats{status} =~ s/Active/Run/; $stats{status} =~ s/Pace_Laps/Pace/; # convert time from MM:SS to seconds $stats{last_lap} = $self->time_to_dec($stats{last_lap}); $stats{best_lap} = $self->time_to_dec($stats{best_lap}); $stats{id} = $stats{car}; #Logger->log(Dumper(%stats)); push @{$session{positions}}, \%stats; } elsif (/^</) { # Header my @values = split(/\|/); my $flag; my $abbrv = $values[5]; if ($abbrv eq 'G') { $flag = 'Green' } elsif ($abbrv eq 'Y') { $flag = 'Yellow' } elsif ($abbrv eq 'R') { $flag = 'Red' } elsif ($abbrv eq 'C') { $flag = 'Checkered' } elsif ($abbrv eq 'U') { $flag = 'Unflagged' } else { $flag = "$abbrv"; } if ($abbrv ne '') { $session{flag} = $flag }; my $msg = $values[16]; $msg =~ s/^>//; $msg =~ s/^.* : //; $session{control_message} = $msg unless $msg =~ /^\S+ flag/i; $session{event} = $values[0]; $session{event} =~ s/\<\!(.*)/$1/; } else { #print "garbage: $_\n"; } } Logger->log("series: |$session{series}| event: |$session{event}| f +lag: |$session{flag}| time: |$session{time}|"); return %session; }
Feel free to provide me name of some high level concept that I can research further.
Thanks, John
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Idiomize This - Cleanup/Transform
by choroba (Cardinal) on Apr 15, 2012 at 20:25 UTC | |
by johnr (Acolyte) on Apr 15, 2012 at 20:35 UTC | |
|
Re: Idiomize This - Cleanup/Transform
by tobyink (Canon) on Apr 15, 2012 at 20:28 UTC | |
|
Re: Idiomize This - Cleanup/Transform
by Anonymous Monk on Apr 16, 2012 at 02:10 UTC | |
by johnr (Acolyte) on Apr 16, 2012 at 23:30 UTC | |
|
Re: Idiomize This - Cleanup/Transform
by jwkrahn (Abbot) on Apr 16, 2012 at 00:28 UTC |