Hello, I have this sub that seems way too long. It loops through a data file with pipe separated values and removes unwanted stuff and transforms values into standard format used elsewhere in application.

I have another one of these subroutines (in another module) that gets html as input and iterates HTML::TableExtract output in the foreach lop.

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; }
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.

Feel free to provide me name of some high level concept that I can research further.

Thanks, John

In reply to Idiomize This - Cleanup/Transform by johnr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.