Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Newbie question

by karlgoethebier (Abbot)
on Aug 23, 2022 at 19:33 UTC ( [id://11146332]=note: print w/replies, xml ) Need Help??


in reply to Newbie question

Yet another inevitable TMTOWTDI:

#!/usr/bin/env perl use strict; use warnings; use feature qw(say); use Math::Prime::Util qw(sumdigits); use Data::Dump; my @input = (-222, -221, -21, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144); my @ans = map { (sumdigits($_) % 2) ? $_ : () } @input; dd \@ans; __END__ Karls-Mac-mini:monks karl$ ./11146306.pl [-221, -21, 1, 1, 3, 5, 21, 34, 89, 144]

See also map, Data::Dump, Math::Prime::Util and ntheory.

Best regards, Karl

«The Crux of the Biscuit is the Apostrophe»

Replies are listed 'Best First'.
Re^2: Newbie question
by LanX (Saint) on Aug 23, 2022 at 19:40 UTC
    > Yet another inevitable TMTOWTDI:

    KISS! s/map/grep/ ;-)

    my @ans = map { (sumdigits($_) % 2) ? $_ : () } @input;

    ... my @ans = grep { sumdigits($_) % 2 } @input; ...

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      Or even:
          my @ans = grep 1 & sumdigits($_), @input;


      Give a man a fish:  <%-{-{-{-<

      "Monachus quidem varietas maxime delectat". Or so.

      «The Crux of the Biscuit is the Apostrophe»

        Excellent quote karlgoethebier! After all, it *is* the spice of life. :)

        "It's not how hard you work, it's how much you get done."

Re^2: Newbie question
by oldB51 (Sexton) on Aug 25, 2022 at 06:52 UTC

    These are incredible modules Karl. Clearly it is important to know what pre built modules are available. That said, at this stage of my Perl journey, I'm glad I built my programme without the modules first. Thanks for you help.

      Yes, sure. Some more inspiration might be found here. Sometimes it is good to take a look under a hood.

      Regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11146332]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-28 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found