Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

color and newline problem (Term::ANSIColor)

35 views
Skip to first unread message

Francesco Stablum

unread,
Dec 31, 2009, 8:05:31 AM12/31/09
to begi...@perl.org
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]

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

Shawn H Corey

unread,
Dec 31, 2009, 9:08:50 AM12/31/09
to Francesco Stablum, begi...@perl.org
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>){

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.

Jeremiah Foster

unread,
Dec 31, 2009, 10:35:38 AM12/31/09
to Francesco Stablum, begi...@perl.org

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 "$. $_";
}
}

0 new messages