On Dec 31, 2009, at 14:05, Francesco Stablum wrote: > Hello everyone, > > i'm a newbie, and I'm trying to write a simple perl script that takes > the stdin and prints it to stdout adding colors that alternates on odd > and even lines. > The problem is that the newline charachter seems to align to the next > line colors and not of the current. > > [code] > use Term::ANSIColor; > foreach(<STDIN>){ > $c = (++$i % 2 == 0)? 'white on_black' : 'black on_white'; > print colored ($_, $c); > } > [/code]
Perl actually has a special variable called $. which is the current line number (so you don't need to count line numbers with i. Here is an example of using that variable, making the whole program more "perlish". #!/usr/bin/perl -w use strict; use Term::ANSIColor; # change the output color of every other line while (<STDIN>) { if ($. % 2) { print color 'white'; print "$. $_"; } else { print color 'yellow'; print "$. $_"; } } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/