I thought I knew this, but...
I thought I knew this, but...
am 26.02.2009 23:04:40 von Barry Brevik
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@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: I thought I knew this, but...
am 26.02.2009 23:12:41 von Jan Dubois
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@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: I thought I knew this, but...
am 26.02.2009 23:20:18 von Serguei Trouchelle
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@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: I thought I knew this, but...
am 26.02.2009 23:35:06 von Andy_Bach
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@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"
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@wiwb.uscourts.gov
Voice: (608) 261-5738; Cell: (608) 658-1890
Entropy just ain't what it used to be
--=_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@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0784272049==--