Any other way to do this

--000000000000bbfa5705ae2e59f9
Content-Type: text/plain; charset="UTF-8"

Please see the following script that checks for type and sums up only the
numbers passed as arguments to the script in the command line.  I would be
grateful if any improvement or furtherance to this script is offered.
Thank you.

#
# sums the numbers given in command line arguments and prints
#
my $sum = 0;
for @*ARGS
{
$sum += $_.Rat if $_.Int // 0 !=== 0;
}
say $sum;

#
# command line execution
# perl6 cla.p6 100 200 300 apples 400oranges 2kilos 18.7876 500 grams14
10stars10 sun100moon 77
#

--000000000000bbfa5705ae2e59f9
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Please see the following script that checks for type and s=
ums up only the numbers passed as arguments to the script in the command li=
ne.=C2=A0 I would be grateful if any improvement or furtherance to this scr=
ipt is offered.=C2=A0 Thank you.<div><br></div><div>#<br># sums the numbers=
 given in command line arguments and prints<br>#<br>my $sum =3D 0;<br>for @=
*ARGS<br>{<br>	$sum +=3D $_.Rat if $_.Int // 0 !=3D=3D=3D 0;<br>}<br>say $s=
um;<br><br>#<br># command line execution<br># perl6 cla.p6 100 200 300 appl=
es 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77<br>#<br></=
div></div>

--000000000000bbfa5705ae2e59f9--
0
weorkay
8/31/2020 3:49:29 PM
perl.perl6.users 1473 articles. 0 followers. Follow

9 Replies
13 Views

Similar Articles

[PageSpeed] 24

I think it looks very good, Radhakrishnan! Presumably you are happy
with the sum 1195.7876?

~$ raku -e 'for @*ARGS {.say if ($_.Int // 0) };' 0 100 200 300 apples
400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
100
200
300
18.7876
500
77

I'm still mulling over whether or not the "!=== 0" is essential.  I
have yet to mess-up the command line arguments sufficiently to require
it, and throwing a zero onto the command line seems to be handled
gracefully.

Anyone else want to chime in?

Best, Bill.

On Mon, Aug 31, 2020 at 8:49 AM Radhakrishnan Venkataraman
<weorkay@gmail.com> wrote:
>
> Please see the following script that checks for type and sums up only the numbers passed as arguments to the script in the command line.  I would be grateful if any improvement or furtherance to this script is offered.  Thank you.
>
> #
> # sums the numbers given in command line arguments and prints
> #
> my $sum = 0;
> for @*ARGS
> {
> $sum += $_.Rat if $_.Int // 0 !=== 0;
> }
> say $sum;
>
> #
> # command line execution
> # perl6 cla.p6 100 200 300 apples 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
> #
0
perl6
8/31/2020 5:22:59 PM
my $is_a_number =3D / ^ \d+ [ '.' \d* ]? $ /;

my $sum =3D @*ARGS.grep($is_a_number).sum;

say $sum;

=E2=80=94=20
Hope this helps,
Bruce Gray (Util of PerlMonks)

> On Aug 31, 2020, at 12:22 PM, William Michels via perl6-users =
<perl6-users@perl.org> wrote:
>=20
> I think it looks very good, Radhakrishnan! Presumably you are happy
> with the sum 1195.7876?
>=20
> ~$ raku -e 'for @*ARGS {.say if ($_.Int // 0) };' 0 100 200 300 apples
> 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
> 100
> 200
> 300
> 18.7876
> 500
> 77
>=20
> I'm still mulling over whether or not the "!=3D=3D=3D 0" is essential. =
 I
> have yet to mess-up the command line arguments sufficiently to require
> it, and throwing a zero onto the command line seems to be handled
> gracefully.
>=20
> Anyone else want to chime in?
>=20
> Best, Bill.
>=20
> On Mon, Aug 31, 2020 at 8:49 AM Radhakrishnan Venkataraman
> <weorkay@gmail.com> wrote:
>>=20
>> Please see the following script that checks for type and sums up only =
the numbers passed as arguments to the script in the command line.  I =
would be grateful if any improvement or furtherance to this script is =
offered.  Thank you.
>>=20
>> #
>> # sums the numbers given in command line arguments and prints
>> #
>> my $sum =3D 0;
>> for @*ARGS
>> {
>> $sum +=3D $_.Rat if $_.Int // 0 !=3D=3D=3D 0;
>> }
>> say $sum;
>>=20
>> #
>> # command line execution
>> # perl6 cla.p6 100 200 300 apples 400oranges 2kilos 18.7876 500 =
grams14 10stars10 sun100moon 77
>> #
0
robertbrucegray3
8/31/2020 6:28:16 PM
I like Bruce's Regex-based approach.=0A=0AHere's how I'd probably approac=
h the problem:=0A=0Araku -e 'say [+] @*ARGS.grep(+*)' 0 100 200 300 apple=
s 400oranges 2kilos=0A18.7876 500 grams14 10stars10 sun100moon 77=0A=0AAu=
gust 31, 2020 2:28 PM, "Bruce Gray" <robertbrucegray3@gmail.com> wrote:=
=0A=0A> my $is_a_number =3D / ^ \d+ [ '.' \d* ]? $ /;=0A> =0A> my $sum =
=3D @*ARGS.grep($is_a_number).sum;=0A> =0A> say $sum;=0A> =0A> =E2=80=94 =
=0A> Hope this helps,=0A> Bruce Gray (Util of PerlMonks)=0A> =0A>> On Aug=
 31, 2020, at 12:22 PM, William Michels via perl6-users <perl6-users@perl=
..org> wrote:=0A>> =0A>> I think it looks very good, Radhakrishnan! Presum=
ably you are happy=0A>> with the sum 1195.7876?=0A>> =0A>> ~$ raku -e 'fo=
r @*ARGS {.say if ($_.Int // 0) };' 0 100 200 300 apples=0A>> 400oranges =
2kilos 18.7876 500 grams14 10stars10 sun100moon 77=0A>> 100=0A>> 200=0A>>=
 300=0A>> 18.7876=0A>> 500=0A>> 77=0A>> =0A>> I'm still mulling over whet=
her or not the "!=3D=3D=3D 0" is essential. I=0A>> have yet to mess-up th=
e command line arguments sufficiently to require=0A>> it, and throwing a =
zero onto the command line seems to be handled=0A>> gracefully.=0A>> =0A>=
> Anyone else want to chime in?=0A>> =0A>> Best, Bill.=0A>> =0A>> On Mon,=
 Aug 31, 2020 at 8:49 AM Radhakrishnan Venkataraman=0A>> <weorkay@gmail.c=
om> wrote:=0A>>> Please see the following script that checks for type and=
 sums up only the numbers passed as=0A>>> arguments to the script in the =
command line. I would be grateful if any improvement or furtherance=0A>>>=
 to this script is offered. Thank you.=0A>>> =0A>>> #=0A>>> # sums the nu=
mbers given in command line arguments and prints=0A>>> #=0A>>> my $sum =
=3D 0;=0A>>> for @*ARGS=0A>>> {=0A>>> $sum +=3D $_.Rat if $_.Int // 0 !=
=3D=3D=3D 0;=0A>>> }=0A>>> say $sum;=0A>>> =0A>>> #=0A>>> # command line =
execution=0A>>> # perl6 cla.p6 100 200 300 apples 400oranges 2kilos 18.78=
76 500 grams14 10stars10 sun100moon 77=0A>>>=20#
0
daniel
8/31/2020 7:23:35 PM
Very nice, Bruce and Daniel!

I continued to hack on Rahakrishnan's code, then realized I could try
using Bruce's grep() call as a filter:

~$ raku -e '@*ARGS.words.grep(*.Rat).sum.say;'  100 200 300 apples
400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
1195.7876

HTH, Bill.

On Mon, Aug 31, 2020 at 12:23 PM <daniel@codesections.com> wrote:
>
> I like Bruce's Regex-based approach.
>
> Here's how I'd probably approach the problem:
>
> raku -e 'say [+] @*ARGS.grep(+*)' 0 100 200 300 apples 400oranges 2kilos
> 18.7876 500 grams14 10stars10 sun100moon 77
>
> August 31, 2020 2:28 PM, "Bruce Gray" <robertbrucegray3@gmail.com> wrote:
>
> > my $is_a_number =3D / ^ \d+ [ '.' \d* ]? $ /;
> >
> > my $sum =3D @*ARGS.grep($is_a_number).sum;
> >
> > say $sum;
> >
> > =E2=80=94
> > Hope this helps,
> > Bruce Gray (Util of PerlMonks)
> >
> >> On Aug 31, 2020, at 12:22 PM, William Michels via perl6-users <perl6-u=
sers@perl.org> wrote:
> >>
> >> I think it looks very good, Radhakrishnan! Presumably you are happy
> >> with the sum 1195.7876?
> >>
> >> ~$ raku -e 'for @*ARGS {.say if ($_.Int // 0) };' 0 100 200 300 apples
> >> 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
> >> 100
> >> 200
> >> 300
> >> 18.7876
> >> 500
> >> 77
> >>
> >> I'm still mulling over whether or not the "!=3D=3D=3D 0" is essential.=
 I
> >> have yet to mess-up the command line arguments sufficiently to require
> >> it, and throwing a zero onto the command line seems to be handled
> >> gracefully.
> >>
> >> Anyone else want to chime in?
> >>
> >> Best, Bill.
> >>
> >> On Mon, Aug 31, 2020 at 8:49 AM Radhakrishnan Venkataraman
> >> <weorkay@gmail.com> wrote:
> >>> Please see the following script that checks for type and sums up only=
 the numbers passed as
> >>> arguments to the script in the command line. I would be grateful if a=
ny improvement or furtherance
> >>> to this script is offered. Thank you.
> >>>
> >>> #
> >>> # sums the numbers given in command line arguments and prints
> >>> #
> >>> my $sum =3D 0;
> >>> for @*ARGS
> >>> {
> >>> $sum +=3D $_.Rat if $_.Int // 0 !=3D=3D=3D 0;
> >>> }
> >>> say $sum;
> >>>
> >>> #
> >>> # command line execution
> >>> # perl6 cla.p6 100 200 300 apples 400oranges 2kilos 18.7876 500 grams=
14 10stars10 sun100moon 77
> >>> #
0
perl6
8/31/2020 8:02:37 PM
Daniel=E2=80=99s solution taught me to not ignore the simple numeric =
coercion operator: `+`.
I was trying to improve version just now, using `0 + $_` in a grep, and =
got hung up on the error thrown on failed numeric conversion.
I ended up with:
	.grep: { try 0 + $_ };
Which does work, and might even be better than .grep(+*) , but I totally =
missed the coercion as a shortcut.

A few other thoughts:
	* The original code fails to handle `0.01`, because (0.01).Int =
is exactly Zero, which fails the `!=3D=3D=3D 0` before .Rat is ever =
called.
	* My Regex fails to handle negative numbers, just due to my =
oversight. It also does not handle 0.3 expressed without the leading =
zero: `.3`
	* All the non-regex solutions (including the original) have =
unexpected effects due to the use of Raku=E2=80=99s numeric conversion; =
none of these are rejected:
		0xDEADBEEF
		1e15
		0b11
		-0o77
		15/60
	* If the job of the code is =E2=80=9C# sums the numbers=E2=80=A6=E2=
=80=9D, then we might need a clearer definition of Number.
		Is it OK to accept whatever Raku can understand as a =
number?
		If not, then a stricter filter must be implemented.

I finally settled on using `try` instead of numeric coercion, because if =
I am not golfing, I think `try` makes the grep look more like =E2=80=9Cfil=
ter out the non-numbers=E2=80=9D instead of =E2=80=9Cget rid of the zero =
values=E2=80=9D.
Also, I am using a `sub MAIN` with a comment that binds to `WHY` to give =
a better command-line usage message, and using named intermediate =
variables to clarify what we are trying to achieve.


#|(Print the sum of the command-line arguments, but only those arguments =
that are valid numbers)
sub MAIN ( *@possible_numbers ) {
    die $*USAGE if not @possible_numbers;

    my @actual_numbers =3D @possible_numbers.grep: { try 0 + $_ };
   =20
    say @actual_numbers.sum;
}


Calling with a `=E2=80=94help` flag, or without arguments, now gives:
$ raku ~/T/d20200831/ee25_e.raku --help
Usage:
  /Users/bruce_pro/T/d20200831/ee25_e.raku [<possible_numbers> ...] -- =
Print the sum of the command-line arguments, but only those arguments =
that are valid numbers

=E2=80=94=20
Hope this helps,
Bruce Gray (Util of PerlMonks)

> On Aug 31, 2020, at 2:23 PM, daniel@codesections.com wrote:
>=20
> I like Bruce's Regex-based approach.
>=20
> Here's how I'd probably approach the problem:
>=20
> raku -e 'say [+] @*ARGS.grep(+*)' 0 100 200 300 apples 400oranges =
2kilos
> 18.7876 500 grams14 10stars10 sun100moon 77
>=20
> August 31, 2020 2:28 PM, "Bruce Gray" <robertbrucegray3@gmail.com> =
wrote:
>=20
>> my $is_a_number =3D / ^ \d+ [ '.' \d* ]? $ /;
>>=20
>> my $sum =3D @*ARGS.grep($is_a_number).sum;
>>=20
>> say $sum;
>>=20
>> =E2=80=94=20
>> Hope this helps,
>> Bruce Gray (Util of PerlMonks)
>>=20
>>> On Aug 31, 2020, at 12:22 PM, William Michels via perl6-users =
<perl6-users@perl.org> wrote:
>>>=20
>>> I think it looks very good, Radhakrishnan! Presumably you are happy
>>> with the sum 1195.7876?
>>>=20
>>> ~$ raku -e 'for @*ARGS {.say if ($_.Int // 0) };' 0 100 200 300 =
apples
>>> 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
>>> 100
>>> 200
>>> 300
>>> 18.7876
>>> 500
>>> 77
>>>=20
>>> I'm still mulling over whether or not the "!=3D=3D=3D 0" is =
essential. I
>>> have yet to mess-up the command line arguments sufficiently to =
require
>>> it, and throwing a zero onto the command line seems to be handled
>>> gracefully.
>>>=20
>>> Anyone else want to chime in?
>>>=20
>>> Best, Bill.
>>>=20
>>> On Mon, Aug 31, 2020 at 8:49 AM Radhakrishnan Venkataraman
>>> <weorkay@gmail.com> wrote:
>>>> Please see the following script that checks for type and sums up =
only the numbers passed as
>>>> arguments to the script in the command line. I would be grateful if =
any improvement or furtherance
>>>> to this script is offered. Thank you.
>>>>=20
>>>> #
>>>> # sums the numbers given in command line arguments and prints
>>>> #
>>>> my $sum =3D 0;
>>>> for @*ARGS
>>>> {
>>>> $sum +=3D $_.Rat if $_.Int // 0 !=3D=3D=3D 0;
>>>> }
>>>> say $sum;
>>>>=20
>>>> #
>>>> # command line execution
>>>> # perl6 cla.p6 100 200 300 apples 400oranges 2kilos 18.7876 500 =
grams14 10stars10 sun100moon 77
>>>> #
0
robertbrucegray3
8/31/2020 8:25:06 PM
Sorry Radhakrishnan, for mis-spelling your name in my last post. --B.



On Mon, Aug 31, 2020 at 1:02 PM William Michels <wjm1@caa.columbia.edu> wro=
te:
>
> Very nice, Bruce and Daniel!
>
> I continued to hack on Rahakrishnan's code, then realized I could try
> using Bruce's grep() call as a filter:
>
> ~$ raku -e '@*ARGS.words.grep(*.Rat).sum.say;'  100 200 300 apples
> 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
> 1195.7876
>
> HTH, Bill.
>
> On Mon, Aug 31, 2020 at 12:23 PM <daniel@codesections.com> wrote:
> >
> > I like Bruce's Regex-based approach.
> >
> > Here's how I'd probably approach the problem:
> >
> > raku -e 'say [+] @*ARGS.grep(+*)' 0 100 200 300 apples 400oranges 2kilo=
s
> > 18.7876 500 grams14 10stars10 sun100moon 77
> >
> > August 31, 2020 2:28 PM, "Bruce Gray" <robertbrucegray3@gmail.com> wrot=
e:
> >
> > > my $is_a_number =3D / ^ \d+ [ '.' \d* ]? $ /;
> > >
> > > my $sum =3D @*ARGS.grep($is_a_number).sum;
> > >
> > > say $sum;
> > >
> > > =E2=80=94
> > > Hope this helps,
> > > Bruce Gray (Util of PerlMonks)
> > >
> > >> On Aug 31, 2020, at 12:22 PM, William Michels via perl6-users <perl6=
-users@perl.org> wrote:
> > >>
> > >> I think it looks very good, Radhakrishnan! Presumably you are happy
> > >> with the sum 1195.7876?
> > >>
> > >> ~$ raku -e 'for @*ARGS {.say if ($_.Int // 0) };' 0 100 200 300 appl=
es
> > >> 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
> > >> 100
> > >> 200
> > >> 300
> > >> 18.7876
> > >> 500
> > >> 77
> > >>
> > >> I'm still mulling over whether or not the "!=3D=3D=3D 0" is essentia=
l. I
> > >> have yet to mess-up the command line arguments sufficiently to requi=
re
> > >> it, and throwing a zero onto the command line seems to be handled
> > >> gracefully.
> > >>
> > >> Anyone else want to chime in?
> > >>
> > >> Best, Bill.
> > >>
> > >> On Mon, Aug 31, 2020 at 8:49 AM Radhakrishnan Venkataraman
> > >> <weorkay@gmail.com> wrote:
> > >>> Please see the following script that checks for type and sums up on=
ly the numbers passed as
> > >>> arguments to the script in the command line. I would be grateful if=
 any improvement or furtherance
> > >>> to this script is offered. Thank you.
> > >>>
> > >>> #
> > >>> # sums the numbers given in command line arguments and prints
> > >>> #
> > >>> my $sum =3D 0;
> > >>> for @*ARGS
> > >>> {
> > >>> $sum +=3D $_.Rat if $_.Int // 0 !=3D=3D=3D 0;
> > >>> }
> > >>> say $sum;
> > >>>
> > >>> #
> > >>> # command line execution
> > >>> # perl6 cla.p6 100 200 300 apples 400oranges 2kilos 18.7876 500 gra=
ms14 10stars10 sun100moon 77
> > >>> #
0
perl6
8/31/2020 8:32:50 PM
Bill,

You were correct that `!=3D=3D=3D 0` is redundant in the original code, =
because a numeric will be checked for zero-versus-not-zero in Boolean =
context, and because `=3D=3D` and `=3D=3D=3D` should mean the same thing =
when comparing a bare numeric value.

In your latest version, I want to point out that `.words` should be =
redundant, since the shell will break up the command-line arguments on =
whitespace, before ever handing them to Raku.
Your code gives the same answer without `.words`.

Raku supports the procedural and functional (and OO, too!) paradigms =
very well.
Here is a different version, for readers who have not yet embraced the =
map/grep mindset we have been showing off.

my @nums;
for @*ARGS -> $arg {
    push @nums, $arg if $arg.Rat;
}
say @nums.sum;


--=20
Hope this helps,
Bruce Gray (Util of PerlMonks)


> On Aug 31, 2020, at 3:02 PM, William Michels <wjm1@caa.columbia.edu> =
wrote:
>=20
> Very nice, Bruce and Daniel!
>=20
> I continued to hack on Rahakrishnan's code, then realized I could try
> using Bruce's grep() call as a filter:
>=20
> ~$ raku -e '@*ARGS.words.grep(*.Rat).sum.say;'  100 200 300 apples
> 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
> 1195.7876
>=20
> HTH, Bill.
>=20
> On Mon, Aug 31, 2020 at 12:23 PM <daniel@codesections.com> wrote:
>>=20
>> I like Bruce's Regex-based approach.
>>=20
>> Here's how I'd probably approach the problem:
>>=20
>> raku -e 'say [+] @*ARGS.grep(+*)' 0 100 200 300 apples 400oranges =
2kilos
>> 18.7876 500 grams14 10stars10 sun100moon 77
>>=20
>> August 31, 2020 2:28 PM, "Bruce Gray" <robertbrucegray3@gmail.com> =
wrote:
>>=20
>>> my $is_a_number =3D / ^ \d+ [ '.' \d* ]? $ /;
>>>=20
>>> my $sum =3D @*ARGS.grep($is_a_number).sum;
>>>=20
>>> say $sum;
>>>=20
>>> =E2=80=94
>>> Hope this helps,
>>> Bruce Gray (Util of PerlMonks)
>>>=20
>>>> On Aug 31, 2020, at 12:22 PM, William Michels via perl6-users =
<perl6-users@perl.org> wrote:
>>>>=20
>>>> I think it looks very good, Radhakrishnan! Presumably you are happy
>>>> with the sum 1195.7876?
>>>>=20
>>>> ~$ raku -e 'for @*ARGS {.say if ($_.Int // 0) };' 0 100 200 300 =
apples
>>>> 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
>>>> 100
>>>> 200
>>>> 300
>>>> 18.7876
>>>> 500
>>>> 77
>>>>=20
>>>> I'm still mulling over whether or not the "!=3D=3D=3D 0" is =
essential. I
>>>> have yet to mess-up the command line arguments sufficiently to =
require
>>>> it, and throwing a zero onto the command line seems to be handled
>>>> gracefully.
>>>>=20
>>>> Anyone else want to chime in?
>>>>=20
>>>> Best, Bill.
>>>>=20
>>>> On Mon, Aug 31, 2020 at 8:49 AM Radhakrishnan Venkataraman
>>>> <weorkay@gmail.com> wrote:
>>>>> Please see the following script that checks for type and sums up =
only the numbers passed as
>>>>> arguments to the script in the command line. I would be grateful =
if any improvement or furtherance
>>>>> to this script is offered. Thank you.
>>>>>=20
>>>>> #
>>>>> # sums the numbers given in command line arguments and prints
>>>>> #
>>>>> my $sum =3D 0;
>>>>> for @*ARGS
>>>>> {
>>>>> $sum +=3D $_.Rat if $_.Int // 0 !=3D=3D=3D 0;
>>>>> }
>>>>> say $sum;
>>>>>=20
>>>>> #
>>>>> # command line execution
>>>>> # perl6 cla.p6 100 200 300 apples 400oranges 2kilos 18.7876 500 =
grams14 10stars10 sun100moon 77
>>>>> #
0
robertbrucegray3
8/31/2020 8:42:29 PM
I like your script, Bruce. And you are quite correct--my code seems to
work just fine without ".words":

~$ raku -e 'say @*ARGS.grep(*.Rat).sum;'

Best, Bill.


On Mon, Aug 31, 2020 at 1:42 PM Bruce Gray <robertbrucegray3@gmail.com> wro=
te:
>
> Bill,
>
> You were correct that `!=3D=3D=3D 0` is redundant in the original code, b=
ecause a numeric will be checked for zero-versus-not-zero in Boolean contex=
t, and because `=3D=3D` and `=3D=3D=3D` should mean the same thing when com=
paring a bare numeric value.
>
> In your latest version, I want to point out that `.words` should be redun=
dant, since the shell will break up the command-line arguments on whitespac=
e, before ever handing them to Raku.
> Your code gives the same answer without `.words`.
>
> Raku supports the procedural and functional (and OO, too!) paradigms very=
 well.
> Here is a different version, for readers who have not yet embraced the ma=
p/grep mindset we have been showing off.
>
> my @nums;
> for @*ARGS -> $arg {
>     push @nums, $arg if $arg.Rat;
> }
> say @nums.sum;
>
>
> --
> Hope this helps,
> Bruce Gray (Util of PerlMonks)
>
>
> > On Aug 31, 2020, at 3:02 PM, William Michels <wjm1@caa.columbia.edu> wr=
ote:
> >
> > Very nice, Bruce and Daniel!
> >
> > I continued to hack on Rahakrishnan's code, then realized I could try
> > using Bruce's grep() call as a filter:
> >
> > ~$ raku -e '@*ARGS.words.grep(*.Rat).sum.say;'  100 200 300 apples
> > 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
> > 1195.7876
> >
> > HTH, Bill.
> >
> > On Mon, Aug 31, 2020 at 12:23 PM <daniel@codesections.com> wrote:
> >>
> >> I like Bruce's Regex-based approach.
> >>
> >> Here's how I'd probably approach the problem:
> >>
> >> raku -e 'say [+] @*ARGS.grep(+*)' 0 100 200 300 apples 400oranges 2kil=
os
> >> 18.7876 500 grams14 10stars10 sun100moon 77
> >>
> >> August 31, 2020 2:28 PM, "Bruce Gray" <robertbrucegray3@gmail.com> wro=
te:
> >>
> >>> my $is_a_number =3D / ^ \d+ [ '.' \d* ]? $ /;
> >>>
> >>> my $sum =3D @*ARGS.grep($is_a_number).sum;
> >>>
> >>> say $sum;
> >>>
> >>> =E2=80=94
> >>> Hope this helps,
> >>> Bruce Gray (Util of PerlMonks)
> >>>
> >>>> On Aug 31, 2020, at 12:22 PM, William Michels via perl6-users <perl6=
-users@perl.org> wrote:
> >>>>
> >>>> I think it looks very good, Radhakrishnan! Presumably you are happy
> >>>> with the sum 1195.7876?
> >>>>
> >>>> ~$ raku -e 'for @*ARGS {.say if ($_.Int // 0) };' 0 100 200 300 appl=
es
> >>>> 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
> >>>> 100
> >>>> 200
> >>>> 300
> >>>> 18.7876
> >>>> 500
> >>>> 77
> >>>>
> >>>> I'm still mulling over whether or not the "!=3D=3D=3D 0" is essentia=
l. I
> >>>> have yet to mess-up the command line arguments sufficiently to requi=
re
> >>>> it, and throwing a zero onto the command line seems to be handled
> >>>> gracefully.
> >>>>
> >>>> Anyone else want to chime in?
> >>>>
> >>>> Best, Bill.
> >>>>
> >>>> On Mon, Aug 31, 2020 at 8:49 AM Radhakrishnan Venkataraman
> >>>> <weorkay@gmail.com> wrote:
> >>>>> Please see the following script that checks for type and sums up on=
ly the numbers passed as
> >>>>> arguments to the script in the command line. I would be grateful if=
 any improvement or furtherance
> >>>>> to this script is offered. Thank you.
> >>>>>
> >>>>> #
> >>>>> # sums the numbers given in command line arguments and prints
> >>>>> #
> >>>>> my $sum =3D 0;
> >>>>> for @*ARGS
> >>>>> {
> >>>>> $sum +=3D $_.Rat if $_.Int // 0 !=3D=3D=3D 0;
> >>>>> }
> >>>>> say $sum;
> >>>>>
> >>>>> #
> >>>>> # command line execution
> >>>>> # perl6 cla.p6 100 200 300 apples 400oranges 2kilos 18.7876 500 gra=
ms14 10stars10 sun100moon 77
> >>>>> #
>
0
perl6
9/1/2020 3:09:10 AM
On Monday, August 31, Bruce Gray wrote:=20
> I finally settled on using `try` instead of numeric coercion, because
> if I am not golfing, I think `try` makes the grep look more like
> =E2=80=9Cfilter out the non-numbers=E2=80=9D instead of =E2=80=9Cget ri=
d of the zero values=E2=80=9D.

Another option is to use 'val' -- which parses it as a literal --

  $ raku -e 'say @*ARGS.map: { val($_) ~~ Numeric }' 1 2 3 a 1e10
   (True True True False True)

Brian
0
bduggan
9/1/2020 3:26:55 PM
Reply: