I thought I knew this, but...
Often I will test some base-level part of a Perl routine I am writing,
even if it is something I already know, just to make sure it really
works as expected. I find that this makes later debugging easier.
Anyway, I need to count how many 'records' I have in a hash after my
program gets done stuffing values into it. So I wrote a piece of test
code that goes like this:
------------------------
use warnings;
%parts = {};
$parts{'ms51957-101'} = 1207;
$parts{'ms51957-102'} = 17;
$parts{'ms51957-103'} = 1;
print "Number of Records in hash: ", scalar keys %parts, "\n";
Number of Records in hash: 4
------------------------
To my surprise, this is wrong; there are only 3 records in the hash.
NOW for the weird part. On a hunch, I changed the line:
%parts = {};
....to...
%parts = ();
Now I get:
Number of Records in hash: 3
....which is correct. Anyone out there know what gives?? And which is the
"correct" way to define an empty hash, {} or ()?
Barry Brevik
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: I thought I knew this, but...
On Thu, 26 Feb 2009, Barry Brevik wrote:
>
> Often I will test some base-level part of a Perl routine I am writing,
> even if it is something I already know, just to make sure it really
> works as expected. I find that this makes later debugging easier.
>
> Anyway, I need to count how many 'records' I have in a hash after my
> program gets done stuffing values into it. So I wrote a piece of test
> code that goes like this:
> ------------------------
> use warnings;
>
> %parts = {};
>
> $parts{'ms51957-101'} = 1207;
> $parts{'ms51957-102'} = 17;
> $parts{'ms51957-103'} = 1;
>
> print "Number of Records in hash: ", scalar keys %parts, "\n";
>
> Number of Records in hash: 4
> ------------------------
>
> To my surprise, this is wrong; there are only 3 records in the hash.
Didn't you get a warning from the program, something like " Reference
found where even-sized list expected"?
> NOW for the weird part. On a hunch, I changed the line:
>
> %parts = {};
This is the same as
%parts = ({}, undef);
Which creates a single hash element that has the stringified version of a hash
reference, e.g. "HASH(0x239bac)" and a value of undef.
> ...to...
>
> %parts = ();
>
> Now I get:
>
> Number of Records in hash: 3
>
> ...which is correct. Anyone out there know what gives?? And which is the
> "correct" way to define an empty hash, {} or ()?
() is an empty list, {} is a reference to an anonymous empty hash.
Cheers,
-Jan
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: I thought I knew this, but...
Barry Brevik wrote:
> ...which is correct. Anyone out there know what gives?? And which is the
> "correct" way to define an empty hash, {} or ()?
{} is a hashref, so assigning {} to a hash will lead this empty hashref to be a key and value is undefined.
So it's like my %parts = ( {} => undef ); which is obviously wrong.
You may want to just declare this hash using "my %parts;", it will be empty.
By the way, if you "use warnings", you should get a warning about it, "Reference found where even-sized list expected".
--
Serguei Trouchelle
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: I thought I knew this, but...
This is a multipart message in MIME format.
--===============0784272049==
Content-Type: multipart/alternative;
boundary="=_alternative 007BEA9E86257569_="
This is a multipart message in MIME format.
--=_alternative 007BEA9E86257569_=
Content-Type: text/plain; charset="US-ASCII"
use warnings;
%parts = {};
$parts{'ms51957-101'} = 1207;
$parts{'ms51957-102'} = 17;
$parts{'ms51957-103'} = 1;
print "Number of Records in hash: ", scalar keys %parts, "\n";
Number of Records in hash: 4
------------------------
> To my surprise, this is wrong; there are only 3 records in the hash.
not quite - {} is an empty, anonymous hash, assigned as a key to the first
entry in %parts. undef is the value. Should've seen:
Reference found where even-sized list expected at -e line 3.
%parts = ();
clears it but isn't needed if parts is new:
my %parts;
will ensure a new, empty hash.
a
-------------------
Andy Bach
Systems Mangler
Internet: andy_bach [at] wiwb.uscourts.gov
Voice: (608) 261-5738; Cell: (608) 658-1890
Entropy just ain't what it used to be
--=_alternative 007BEA9E86257569_=
Content-Type: text/html; charset="US-ASCII"
<tt><font size=2>use warnings;<br>
<br>
%parts = {};<br>
<br>
$parts{'ms51957-101'} = 1207;<br>
$parts{'ms51957-102'} = 17;<br>
$parts{'ms51957-103'} = 1;<br>
<br>
print "Number of Records in hash: ", scalar keys %parts, "\n";<br>
<br>
Number of Records in hash: 4<br>
------------------------<br>
<br>
> To my surprise, this is wrong; there are only 3 records in the hash.</font></tt>
<br>
<br><font size=2 face="sans-serif">not quite - {} is an empty, anonymous
hash, assigned as a key to the first entry in %parts. undef is the value.
Should've seen:</font>
<br><font size=2 face="sans-serif">Reference found where even-sized list
expected at -e line 3.</font>
<br>
<br><font size=2 face="sans-serif">%parts = ();</font>
<br>
<br><font size=2 face="sans-serif">clears it but isn't needed if parts
is new:</font>
<br><font size=2 face="sans-serif">my %parts;</font>
<br>
<br><font size=2 face="sans-serif">will ensure a new, empty hash.</font>
<br>
<br><font size=2 face="sans-serif">a</font>
<br><font size=2 face="sans-serif">-------------------<br>
Andy Bach<br>
Systems Mangler<br>
Internet: andy_bach [at] wiwb.uscourts.gov<br>
Voice: (608) 261-5738; Cell: (608) 658-1890<br>
<br>
Entropy just ain't what it used to be</font>
--=_alternative 007BEA9E86257569_=--
--===============0784272049==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0784272049==--