Also, this might help you understand what is going on with
exists $is_prime{$_} and $is_prime{$_}
Run the code to see the differences between exists(), defined and true. See if you can understand the warnings this code gives.
#!/usr/bin/perl
use strict;
use warnings;
use feature qw/ say /;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
my %hash;
$hash{'A'} = 1;
$hash{'B'} = 0;
$hash{'C'} = -1;
$hash{'D'} = undef;
say Data::Dumper->Dump([\%hash], ['*hash']);
for (qw/ A B C D E /) {
say "Key: $_ Val: $hash{$_}";
say exists $hash{$_} ? ' exists' : ' does not exist';
say defined $hash{$_} ? ' defined' : ' not defined';
say $hash{$_} ? ' true' : ' false';
say '- -' x 20;
}
__END__
The way forward always starts with a minimal test.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|