Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: The error says the value is uninitialized, but it works anyway

by AnomalousMonk (Bishop)
on Aug 18, 2019 at 23:40 UTC ( #11104668=note: print w/replies, xml ) Need Help??


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:

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11104668]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2019-11-17 23:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (87 votes). Check out past polls.

    Notices?