It appears you are trying to make one subroutine do the work of several, and distinguishing which by the number and content of your "subargs" (which I will call "parameters" to your sub, to distinguish them from the command line arguments in @ARGV). This one sub does five different things:
- With no parameters passed, counts the number of arguments passed on the command line;
- Otherwise, takes its first parameter and compares it against each command line argument, keeping a count of the number of matches and nonmatches;
- If the second parameter matches a given string, returns the count of matches determined above;
- If the second parameter matches a different string, returns the count of nonmatches given above; and
- If the second parameter matches yet something else, tries to do something I can't figure out. Since this is never tested by the test cases given, I'm going to ignore this case entirely and leave it to you and to the other monks.
I would structure this monster as a number of different subroutines. Basically, get rid of the second parameter to your catch-all sub; then write one subroutine to do each of the five tasks above, using the sub's name to convey the same information. The first sub, however, is probably better written right into your mainline code, as it operates on the global @ARGV -- for now. The second sub would be called in turn by the following two or three.
Furthermore, I wouldn't necessarily rely on the fact that the list you are searching for resides in @ARGV, for the simple reason that a real program will very likely accept its data from a file or other source rather than as discrete command-line arguments. Copy the arguments into a normal array and pass a reference to this array to the task subroutines above. If you have no idea what I mean by references at the moment, click or run perldoc perlreftut.
Oh, and your uninitialized variables problems will pretty much Go Away if you do two things:
- set all your "my" variables to a value (preferably 0 or "") when you declare them, and
- instead of testing for defined (which you haven't quite got the hang of yet), just test for nonzero/non-null: if ($variable) instead of if (defined $variable).
You're getting there, just not quite there yet.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.