Hello Monks,

I am a beginner programmer, and the use of Strategy(http://www.perl.com/pub/2003/08/07/design2.html) was recommended to make my code better.

The whole idea is a little beyond my knowledge. SO I am just asking for help getting started.

If you could just give me a few hints and point me in the right direction, like Step 1. It would be greatly appreciated!

Thanks!

Here's the portion of the code that is to changed:

#Dependencies (relations and arguments): if ($category_id eq "all") { my @lines = split ("\n",$sentblock); ##Split by a newl +ine foreach my $line (@lines) { my @matches; next unless ($line =~ /\b$verbform\b/i); ##Ensure +dependency contains searchword ##NEXT LINE IS DIFFERENCE: next unless ($line =~ /subj\w*\(|obj\w*\(|prep\w*\ +(|xcomp\w*\(|agent\w*\(|purpcl\w*\(|conj_and\w*\(/); ##Ensure depende +ncy only contains desired grammar relations next unless ($line =~ /(\w+)\((\w+)\-\d+\,\s(\w+)\ +-\d+\)/); ##Ensure dependency is a dependency AND get info from it $grammar_relation = $1; $argument1 = $2; $argument2 = $3; next if ($argument1 eq $argument2); ##Ensure 1st a +nd 2nd argument aren't the same next if ($grammar_relation =~ /xcomp/i and $argume +nt2 !~ /\b$verbform\b/i); ##Ensure for xcomp the searchword is the 2n +d argument next if ($argument1 =~ /^\S$/ or $argument2 =~ /^\ +S$/); ##Exclude if argument is only 1 character push(@matches, $chapternumber, $sentencenumber, $s +entence, $grammar_relation, $argument1, $argument2); ##All here, so e +ither all get pushed or none (no holes in array) push @all_matches, \@matches; } } elsif ($category_id eq "subj") { my @lines = split ("\n",$sentblock); ##Split by a newl +ine foreach my $line (@lines) { my @matches; next unless ($line =~ /\b$verbform\b/i); ##Ensure +dependency contains searchword next unless ($line =~ /subj\w*\(|agent\w*\(/); ##E +nsure dependency only contains desired grammar relations next unless ($line =~ /(\w+)\((\w+)\-\d+\,\s(\w+)\ +-\d+\)/); ##Ensure dependency is a dependency AND get info from it $grammar_relation = $1; $argument1 = $2; $argument2 = $3; next if ($argument1 eq $argument2); ##Ensure 1st a +nd 2nd argument aren't the same next if ($argument1 =~ /^\S$/ or $argument2 =~ /^\ +S$/); ##Exclude if argument is only 1 character push(@matches, $chapternumber, $sentencenumber, $s +entence, $grammar_relation, $argument1, $argument2); push @all_matches, \@matches; } } ...etc... for a few more elsifs #To make the if elsif into subroutine: Name:get_all_matches Pass +In: ($sentblock, $verbform, $chapternumber, $sentencenumber, $sentenc +e) Return: @allmatches (if needed, return reference)

Or just tell me if this is not meant for me yet. Or if I should ask this question better. Thanks for your time.

If you want a full look at my code: (http://codereview.stackexchange.com/questions/2911/to-make-a-subroutine-or-not-general-code-tips)

In reply to How to implement a Design Pattern/ Strategy?? 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.