in reply to Idiomize This - Cleanup/Transform

There is not really much you can idiomize, if you show all the code you want to change. But you can take inspiration from the following (untested):
use constant EVENT => 0; use constant ABBRV => 5; use constant MSG => 16; my %abbrevs = (G => 'Green', Y => 'Yellow', R => 'Red', C => 'Checkered', U => 'Unflagged', ); my %subs = (position => [qr/\+/, ''], # sometimes there is a " ++" driver => [qr/ *\(R\)$/, ''], # I don't care if rookie best_speed => [qr/\+/, ''], # sometimes there is a " ++" at end status => [qr/In Pit/i, 'Pit', qr/Active/, 'Run', qr/Pace_Laps/, 'Pace'], ); sub get_state { my ($self, $contents) = @_; $contents =~ s/\r//g; my %session; ## That's enough foreach (split /\n/, $contents) { if (/^ *\d+\+*\|/) { # Data record my @values = split(/\|/); s/^\ *// foreach @values ; my %stats; @stats{ @{ $self->{fields} } } = @values; # do some clean up / bring to common values if ($stats{status} eq '') { # not all the series track run, pi +t, etc $stats{status} = 'Run' } for my $stat (keys %subs) { my @subst = @{ $subs{$stat} }; while (my ($match, $replace) = splice @subst, 0, 2) { $stats{$stat} =~ s/$match/$replace/g; } } # convert time from MM:SS to seconds $stats{$_} = $self->time_to_dec($stats{$_}) for qw/last_lap best_lap/; $stats{id} = $stats{car}; #Logger->log(Dumper(%stats)); push @{ $session{positions} }, \%stats; } elsif (/^</) { # Header my @values = split(/\|/); my $abbrv = $values[ABBRV]; my $flag = exists $abbrevs{$abbrv} ? $abbrevs{$abbrv} : $a +bbrv; if ($abbrv ne '') { $session{flag} = $flag }; my $msg = $values[MSG]; $msg =~ s/^>//; $msg =~ s/^.* : //; $session{control_message} = $msg unless $msg =~ /^\S+ flag/i; $session{event} = $values[EVENT]; $session{event} =~ s/\<\!(.*)/$1/; } else { #print "garbage: $_\n"; } } ## One more space at the end Logger->log(join '| ', map "$_: |$session{$_}", qw/series event flag time/); return %session; }

Replies are listed 'Best First'.
Re^2: Idiomize This - Cleanup/Transform
by johnr (Acolyte) on Apr 15, 2012 at 20:35 UTC
    Thanks both you...and yes, that is the inspiration (and more) that I needed! John