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]
Is it a flushing problem? I cannot understand why. Do you?
many thanks,
Francesco Stablum
P.S. sorry for my bad english
--
The generation of random numbers is too important to be left to chance
- Robert R. Coveyou
chomp;
my $c = (++$i % 2 == 0)? 'white on_black' : 'black on_white';
print colored ($_, $c), "\n";
> $c = (++$i % 2 == 0)? 'white on_black' : 'black on_white';
> print colored ($_, $c);
> }
> [/code]
>
> Is it a flushing problem? I cannot understand why. Do you?
The \n should be outside of colored().
--
Just my 0.00000002 million dollars worth,
Shawn
Programming is as much about organization and communication
as it is about coding.
I like Perl; it's the only language where you can bless your
thingy.
> 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 "$. $_";
}
}