Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
We haven't learned "map" or "grep" yet.

Others have linked to map and grep and to discussions of the use of these powerful built-in functions for solving your problem, and have given concrete examples of their use. You've had a chance to look at this material, so I'll just go ahead and give another example. This example is given in the context of a Test::More testing framework, which has also been mentioned already. Such a framework allows you to more easily alter functions and test data and more effectively play with different approaches. (Note that the difference set is assigned back to the original  @colors array.)

c:\@Work\Perl\monks>perl use strict; use warnings; use Test::More 'no_plan'; use Test::NoWarnings; my @colors = qw(red green blue yellow pink purple brown); my @drop = qw(pink purple); my %to_be_dropped = map { $_ => 1 } @drop; @colors = grep !$to_be_dropped{$_}, @colors; is_deeply \@colors, [ qw(red green blue yellow brown) ], "dropped: (@drop)"; done_testing; __END__ ok 1 - dropped: (pink purple) 1..1 ok 2 - no warnings 1..2

Ok, that works, let's go nuts! Let's extend the Test::More framework to add many different test cases. When a software change is made, a large (and growing) number of test cases gathered together in one place/file allows you to quickly confirm that all the stuff that used to work still does, and makes it easy to add just one more test case, perhaps the one that reveals a nasty bug. At the same time, let's introduce:

  • Anonymous array  [ ... ] constructors (see perlref);
  • Array reference de-referencing  @$array_reference (also perlref);
  • More complex Perl data structures (see perldsc);
  • Distinguishing between a reference and a non-reference with ref;
  • ...?
c:\@Work\Perl\monks>perl use strict; use warnings; use Test::More 'no_plan'; use Test::NoWarnings; note "add more test cases; it's easy -- and fun!"; my @Tests = ( 'edge cases: drop all, none, nothing matches', [ [ qw(red green blue yellow pink purple brown) ], # input set [ qw(red green blue yellow pink purple brown) ], # drop these [ ], # expected output ], [ [ qw(red green blue yellow pink purple brown) ], [ ], [ qw(red green blue yellow pink purple brown) ], ], [ [ qw(red green blue yellow pink purple brown) ], [ qw(foo bar) ], [ qw(red green blue yellow pink purple brown) ], ], 'degenerate cases: empty input', [ [ ], [ qw(red green blue yellow pink purple brown) ], [ ], ], [ [ ], [ ], [ ], ], 'normal cases', [ [ qw(red green blue yellow pink purple brown) ], [ qw(red ) ], [ qw( green blue yellow pink purple brown) ], ], [ [ qw(red green blue yellow pink purple brown) ], [ qw( brown) ], [ qw(red green blue yellow pink purple ) ], ], [ [ qw(red green blue yellow pink purple brown) ], [ qw( pink purple ) ], [ qw(red green blue yellow brown) ], ], ); VECTOR: for my $ar_vector (@Tests) { if (not ref $ar_vector) { note $ar_vector; next VECTOR; } my ($ar_input, $ar_drop, $ar_expected) = @$ar_vector; my %to_be_dropped = map { $_ => 1 } @$ar_drop; my @output = grep !$to_be_dropped{$_}, @$ar_input; is_deeply \@output, $ar_expected, "dropped: (@$ar_drop)"; } # end for VECTOR done_testing; exit; # define test/support functions after exit (why?) # functions under test (if any), support subroutines ################ # none for now __END__ # add more test cases; it's easy -- and fun! # edge cases: drop all, none, nothing matches ok 1 - dropped: (red green blue yellow pink purple brown) ok 2 - dropped: () ok 3 - dropped: (foo bar) # degenerate cases: empty input ok 4 - dropped: (red green blue yellow pink purple brown) ok 5 - dropped: () # normal cases ok 6 - dropped: (red) ok 7 - dropped: (brown) ok 8 - dropped: (pink purple) 1..8 ok 9 - no warnings 1..9
(This should really have been put into a file. Oh, well...) It's a lot to absorb, so don't worry too much about it. It's really just meant to whet your appetite: just know it's there.


Give a man a fish:  <%-{-{-{-<


In reply to Re^3: The error says the value is uninitialized, but it works anyway by AnomalousMonk
in thread The error says the value is uninitialized, but it works anyway by mizducky

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (3)
    As of 2019-12-12 05:19 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?