http://www.perlmonks.org?node_id=11104668


in reply to Re^2: The error says the value is uninitialized, but it works anyway
in thread The error says the value is uninitialized, but it works anyway

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:

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:  <%-{-{-{-<