in reply to Re: Return 2 arrays, sort the same, and concatenate them
in thread Return 2 arrays, sort the same, and concatenate them

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 :)

Replies are listed 'Best First'.
Re^3: Return 2 arrays, sort the same, and concatenate them
by jonc (Beadle) on Jul 01, 2011 at 15:06 UTC

    Yes, it is helpful that way. More modular.

    I am almost positive that the wrong info (or none) is being pushed into @all_pronoun_matches.

    I came to this conclusion because after using your methods, I got an output that had @all_matches correct. BUT the info in @all_pronoun_matches was messed:

    The heading had the wrong count (didn't sort, just had # of @all_matches), and didn't have $match->[4] and $match[5].

    The matches under the heading didn't have anything (just printed "Section")

    I still can't say why the push isn't working though. THANKS

      Okay, it might be that I am returning in that loop, so the matches aren't being kept! How do I prevent the matches that the condition is true for from getting to @all_matches but reaching @all_pronoun_matches?

      I have tried removing the "return" and putting the @all_matches push statements in else, of the foreach. This didn't work:

      It just sorted as if there were no condition statement to separate them. Like everything went into @all_matches

      For clarity, this is the output I'm looking for:

      2 match(es) in which the subject of move is animals :
      
      Section 1_1: Radially symmetrical animals move slowly or not at all .
      
      Section 1_1: Cnidarians also have epithelial cells with muscle fibers whose contractions enable the animals to move , as well as nerve nets that integrate their body activities .
      
      
      1 match(es) in which the subject of move is cavity :
      
      Section 1_1: Flatworms -LRB- phylum Platyhelminthes -RRB- have no body cavity , lack organs for oxygen transport , have only one entrance to the gut , and move by beating their cilia .
      
      
      2 match(es) in which the subject of move is they :
      
      Section 1_1: Others , the sea butterflies and heteropods , have a modified foot that functions as a swimming organ with which they move through open ocean waters .
      
      Section 1_1: Because fluids are relatively incompressible , they move to another part of the cavity when muscles surrounding them contract .

      Even with another heading: "Here are the pronouns:" before "they" shows up.

        Okay, it might be that I am returning in that loop, so the matches aren't being kept! How do I prevent the matches that the condition is true for from getting to @all_matches but reaching @all_pronoun_matches?

        I think you want next or last instead of return. Return exits a subroutine, last exits a loop.