PilotinControl has asked for the wisdom of the Perl Monks concerning the following question:

Good Afternoon Monks!
Question: I would like to add a different color to each field in just a few lines of code. I have already accomplished this using several lines of code however the process needs to be repeated for each field. The code below just makes everything one color. The goal is to make each field a different color. Thanks in advanced!

my $format = "%-4s %-11s %-13s %-10s %-7s %0s\n"; print color 'bold green'; printf ($format, $field[0], $field[1], $field[2], $field[3], $field[4] +, $field[5]);

Replies are listed 'Best First'.
Re: Print Color Format
by toolic (Bishop) on Jun 16, 2015 at 17:35 UTC
    use warnings; use strict; use Term::ANSIColor; my @field = 1..6; my @fmts = qw(%-4s %-11s %-13s %-10s %-7s %0s); my @cols = qw(green red green red green red); # Fill in your colors he +re for my $i (0 .. $#field) { print color($cols[$i]); printf $fmts[$i], $field[$i]; } print color('reset'), "\n";

      Thanks toolic!
      your code works as is, however when I insert a split code, which removes ":" from each field from the data file, I received an "uninitialized value for $_" error? I am confused as to why that is? Thoughts? Thanks!