Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Newbie question

by Marshall (Canon)
on Aug 23, 2022 at 10:53 UTC ( [id://11146306]=note: print w/replies, xml ) Need Help??


in reply to Newbie question

Looks like homework, but you have done some work and asked for suggestions.

  1. Don't push when you can print.
  2. Usually better to make sub work on one number rather than an array of numbers.
  3. Always use strict and use warnings.
  4. You don't have to predeclare the sub -> Ok to put the loop on the input first.
  5. If I have a good name for the sub, I may not have to be too interested in how it works. With this ordering, I see right away what the program is supposed to do.
My version:
use strict; use warnings; my @input = ( -222, -221, -21, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 +, 144 ); foreach my $int (@input) { print "$int " if isSumDigitsOdd($int); } print "\n"; sub isSumDigitsOdd { my $num = shift; my $sum; $sum += $_ foreach (split //, abs $num); return ( $sum % 2 ); # return ( $sum & 1 ); would be fine also } #prints: -221 -21 1 1 3 5 21 34 89 144
Update: 1) The name "isSumDigitsOdd" was chosen for a reason. The "is" prefix is one convention for indicating that this returns a flag/boolean value (yes/no). I expect 1 true, 0 false, but tolerate any one bits as true and only all zeroes as false.
2) The foreach loop was also done for a reason..I was able to work in the idea that this expects ints by using $int as the iterator value.
All sorts of details wind up making a difference.

Also:
As an example, I have used the absolute value function abs but I could have used abs($_). Which is best? Does it matter?

I would write abs($_) or even abs $_ rather than just "abs" just to make things more clear even though the generated code is the same. Often there is some descriptive name other than just $_ and I use $_ for simple one line loops like above. Naming loop iterator variables is cheap and improves readability - do it often!

Replies are listed 'Best First'.
Re^2: Newbie question
by jdporter (Paladin) on Aug 24, 2022 at 13:24 UTC
    Don't push when you can print.

    I'm sorry, this is not good advice. It creates a "strong coupling" between the logic of the algorithm and the interface/caller. It may be that you decide later that you want to send the results somewhere else, or do further processing on them before outputting. Do you really want to dive into the algorithm and find all the print statements? You'll just be converting them to push'es anyway. Better advice is to have your algorithm build its results in a data structure; have a separate routine for outputting that data.

      Thank you for this jdporter.

Re^2: Newbie question
by oldB51 (Sexton) on Aug 23, 2022 at 12:41 UTC

    Your practical comments are extremely helpful.

    Not sure what the 'looks like homework' jibe was meant to achieve. I'm 71, have been retired for nearly 14 years and I don't do homework in the sense that term is usually understood. I have been learning Perl for 6 days.

    Nonetheless your other comments are extremely useful and I thank you for them.

      Oh, sometimes we get a student who are lazy and trying to trick us into doing their homework for them. But usually those folks don't show up with anything close to working code!

      After your explanation, I am impressed! Your code demonstrates a level of understanding that extremely few 6-day beginners possess. And your question wasn't "how do I do it?", it was "how do I do it better?".

      Keep going! You are off to a very fine start!

        Thank you for this observation and, once again, thank you for your earlier substantive comments.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-18 11:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found