Nice job, thinks are looking more answerable already

Ok, here is the next step, start making functions :)

Functions with meaningful names

For an example see Re^6: Help with locating bp region in chromosome

You would turn each loop into a separate function, so you end up with something like

... Main( @ARGV ); exit( 0 ); sub Main { ... my ( $allPronouns, $allMatches ) = MeaningfulMatches( \@parsed ); my $PronounCountsHashref = CountPronouns( $allPronouns ); my $CountsHashref = CountMatches( $all_matches ); SortThisMotherReference( $CountsHashref, $allMatches ); SortThisMotherReference( $PronounCountsHashref, $allPronouns ); ... }

See, isn't that easier to read? And see how the meaningful function names replace some of the comments?

Now that you've got things separated into functions, its easy to use test and tweak each part individually without worrying about the whole thing working, until you've got it producing the correct output.

So the next step is to make sure each function does its job by writing tests for each function. You give it input, it gives you output (or it modifies your input), and you compare the two to see if they match. You can use Test::Deep for this.

Once they match, you move on to the next function

Oh you say CountPronouns() is doing an inaccurate count? Or SortThisMotherReference() is not sorting correctly?

Now its easy to write a small test program. Use Data::Dumper to create sample input, modify it by hand until you have sample output, and then modify the function until it produces the same output.

For another example see Re^3: SEO Fixer Part II - Updated or watch this ~14min video String Calculator TDD Kata done in Perl of a programmer doing this live :)


In reply to Re^2: Return 2 arrays, sort the same, and concatenate them by Anonymous Monk
in thread Return 2 arrays, sort the same, and concatenate them by jonc

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.