I had to sleep on it and then the solution was stupidly obvious (as I knew it would be).
However, now I'm having the problem I didn't think I would have - the ANSI sequences are showing up as printable (as well as taking some effect) in the print. However the escape that should turn the text back to default isn't showing up or having any effect. #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $line = 'foo bar baz ball'; my $match = [ [ [ '0' ], [ '7' ] ] ]; my $color = $ENV{GREP_COLOR}; # The environment looks like 1;32 but we need to send \e[32m $color =~ s/1;(.*)/\e[$1m/; print "match " . Dumper($match); # Each regex foreach my $ereg (@$match) { my $enum = ($#{$ereg->[0]} >= $#{$ereg->[1]} ? $#{$ereg->[0]} : $#{$ereg->[1]}); print "ereg " . Dumper(@$ereg); print "blah " . $ereg->[0][0] . "\n"; # Each match foreach my $i (0 .. $enum) { substr($line, $ereg->[0][$enum], 0, $color); substr($line, $ereg->[1][$enum], 0, '\e[0m'); } } print $line On Thu, Oct 3, 2013 at 8:24 PM, Rob Dixon <rob.di...@gmx.com> wrote: > shawn wilson <ag4ve...@gmail.com> wrote: > >From position 0 to 7 should be whatever GREP_COLOR export is defined >>as. There might be issues with how I'm doing that but my main issue (I >>think) is how I'm looping (and/or how I'm using substr). >> >>#!/usr/bin/perl >> >>use strict; >>use warnings; >> >>use Data::Dumper; >> >>my $line = 'foo bar baz ball'; >>my $match = [ >> [ >> [ >> '0' >> ], >> [ >> '7' >> ] >> ] >> ]; >> >>my $color = $ENV{GREP_COLOR}; >># The environment looks like 1;32 but we need to send \e[32m >>$color =~ s/1;/\e[/; >>$color .= 'm'; >> >>print "match " . Dumper($match); >># Each regex >>foreach my $ereg (@$match) >>{ >> print "ereg " . Dumper(@$ereg); >> print "blah " . $ereg->[0][0] . "\n"; >> # Each match >> foreach my $epat (@$ereg) >> { >> print "epat " . Dumper(@$epat); >> substr($line, $epat->[0], 0, $color); >> substr($line, $epat->[1], 0, '\\e\[0m'); >> } >>} >> >>print $line > > I'm unclear what you intend, but the `foreach my $epat` executes *twice*, > first with > $epat set to [ '0' ] and then with $epat set to [ '7' ]. > > So in the first iteration $epat->[0] is '0' and in the second iteration it is > '7'. In both cases there > is no $epat->[1], hence the warnings. > > Perhaps what you want is $ereg->[0][0] and $ereg->[1][0]? > > Rob > > (Sorry about this awful email - I'm using a tablet on a train!) > > > > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/