Indeed an improvement. For contemplation here's something closer to what I would write:

#!/usr/bin/perl use warnings; use strict; use 5.010; # The 3n + 1 problem (http://www.streamtech.nl/problemset/100.html) my @pairs; say "Input:"; while (defined ($_ = <STDIN>) && !/^\.$/) { if (!/^(\d+)\s+(\d+)$/ && $1 > 0 && $2 > 0) { chomp; say "Invalid input >$_<."; say 'Two non-zero positive integers are expected. Input ignore +d'; next; } push @pairs, [$1, $2]; } say "Output:"; for my $pair (@pairs) { my $maxLength = calcCycleLen ($pair->[0]); my $length2 = calcCycleLen ($pair->[1]); $maxLength = $length2 if $maxLength = $length2; say "@$pair $maxLength"; } sub calcCycleLen { my ($number) = @_; my $length = 1; while ($number > 1) { $number = $number % 2 ? $number * 3 + 1 : $number / 2; ++$length; } return $length; }

The first change is to add a stopping condition for command line input from STDIN. while (<$fooIn>) { is fine if $fooIn is connected to a file, but is less good for devices such as a keyboard where the end of file isn't clear.

There are a few other changes of note in the while loop:

  1. the regular expression is used to capture the two numbers and the requirement for just one white space character is relaxed.
  2. A range test is performed on the numbers and provides an additional failure mode for the input
  3. The bad input is reported and the valid input criteria are specified
  4. There is no else part to the if statement. Instead the "early exit" technique is used. This very often reduces nested code and can make the main flow of the code much easier to see.
  5. The pairs are stored in their own two element array. @pairs thus contains arrays - it is an AoA (some people like to use lol instead).

Moving on: as a personal preference I use for everywhere rather than a mixture of for and foreach - probably because I'm lazy.

The @pairs for loop has changed a lot!

  1. $pair now is a reference to an array containing two numbers instead of a string.
  2. $maxLength is initialised to the first value that will be calculated. This trick is often used where a result is calculated by considering each element of a vector in turn. Initialise the result variable with the calculated value from the first element of the vector, then process the remaining elements in a loop.
  3. Calculate the second value. Note the syntax used to access the array elements from the $pair variable holding a reference to the array btw.
  4. Update $maxLength if it is smaller than the second length. Note the use of if as a statement modifier.

Now we are in the home stretch.

  1. I use my ($number) = @_; because it scales better than a bunch of shifts. Not needed in this case, but it's what flows from my fingers.
  2. The C style for loop is almost unused in Perl. In this case a while loop better gives the flavour of what is going on anyway.
  3. The ternary operator is easy to abuse. In this case though it reduces some rather cluttered looking code to something that (in my view) is clear and concise.

The final step is to pass Perl::Tidy over the code to clean up any wayward formatting, then enjoy.

True laziness is hard work

In reply to Re^2: General style advice requested by GrandFather
in thread General style advice requested by iangibson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.