I want to print the even numbers in a row but I can't.
use Terminal::ANSIColor;
# Wanna print even numbers in red
for <1 2 3 4>
{ $_ %2 == 0 ?? say color('red'),$_,color('reset') !! say $_ }
printf
doesn't seem to work with the Terminal::ANSIColor
directives and put
doesn't work either.
Is there any switch to say
which makes it print without newline? How to print those Terminal::ANSIColor
formatted sections in a row?
say
is basically defined as:
sub say ( +@_ ) {
for @_ {
$*OUT.print( $_.gist )
}
$*OUT.print( $*OUT.nl-out );
}
If you don't want the newline, you can either change the value of $*OUT.nl-out
or use print
and gist
.
say $_;
print $_.gist;
In many cases the result of calling .gist
is the same as .Str
. Which means you don't even need to call .gist
.
use Terminal::ANSIColor;
# Wanna print even numbers in red
for <1 2 3 4> {
$_ %% 2 ?? print color('red'), $_, color('reset') !! print $_
}
(Note that I used the evenly divisible by operator %%
.)
say
is for humans, which is why it uses .gist
and adds the newline.
If you want more fine-grained control, don't use say
. Use print
or put
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With