develooper Front page | perl.perl6.users | Postings from January 2019

Re: **@args question

Thread Previous | Thread Next
From:
Richard Hainsworth
Date:
January 3, 2019 08:44
Subject:
Re: **@args question
Message ID:
d51e68d9-2dc1-022a-2841-1a014f3a3404@gmail.com
<snip>
>>
>> The term "slurpy" did help a lot.
>>
>> :-)
>>
>> I am writing your explanation down for my records.
>
>
> Well Golly!
>
> $ p6 'sub printx(**@args){print(@args)}; printx("abc",1,"def\n");'
> abc 1 def
>
> $ p6 'sub printx(**@args){print @args, "\n"}; printx("abc",1,"def");'
> abc 1 def
>
> Question:
> $ p6 'sub printx(**@args){print(@args)}; printx("abc","xyz","def\n");'
> abc xyz def
>
> Are the spaces suppose to be there?

In each case you are using 'print @args', then `printx` adds on a "\n" 
either to the last item or the final string, which in each case results 
in exactly the same string. The way 'print' handles an array is to put a 
space between each item, which can be very confusing when dealing with 
items that are themselves strings with spaces.

For other "stringifications" of an array, try 'print @args.fmt("%s,")' 
if you want a comma, or 'print @args.perl'.

I find '.say for @args' is better because I get an item on each line.

Also 'dd @args' is quite useful.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About