Lars Malmsteen
Lars Malmsteen

Reputation: 768

say without newline in Raku

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?

Upvotes: 6

Views: 318

Answers (2)

Peter K
Peter K

Reputation: 1807

Use print instead of say:

print color('red')

or

print 'red'

Upvotes: 0

Brad Gilbert
Brad Gilbert

Reputation: 34120

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.

Upvotes: 7

Related Questions