Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hash of hashes #425

Closed
p5pRT opened this issue Aug 24, 1999 · 6 comments
Closed

Hash of hashes #425

p5pRT opened this issue Aug 24, 1999 · 6 comments

Comments

@p5pRT
Copy link

p5pRT commented Aug 24, 1999

Migrated from rt.perl.org#1264 (status was 'resolved')

Searchable as RT1264$

@p5pRT
Copy link
Author

p5pRT commented Aug 24, 1999

From kenc@research.canon.com.au

Perl Version​: 5.004_04, 5.005_03
OS​: RedHat Linux 6.0

*****************************************************************
%hash = ();

$hash{10}{5} = 1;

while ($i < 1000000)
{
  $i++;
  $j = $i % 6;
  print "Yeah! $i\n" if (defined $hash{$i}{$j});
}
*****************************************************************

Regards
Ken Carroll

--


Ken Carroll, Senior Software Engineeer
Canon Information Systems Research Australia P/L
Phone​: +61 2 9805 2031 Fax​: +61 2 9805 2929
mailto​:kenc@​research.canon.com.au
http​://www.research.canon.com.au/

@p5pRT
Copy link
Author

p5pRT commented Aug 24, 1999

From [Unknown Contact. See original ticket]

On Wed, 25 Aug 1999 at 03​:59​:11 +1000, Ken Carroll wrote​:

Hi.
Run this program and watch the size of your PERL process grow
uncontrollably. Is this a bug? The variables $i and $j are chosen for
illustration purposes only. There is no logic to this code.

Arguable as to whether it's a bug. Referring to $hash{$i}{$j} - even
with 'defined' - means that a reference to $hash{$i} springs into
existence, so that perl can see if it contains the desired element.

This doesn't leak memory​:

  print "Yeah! $i\n" if (defined $hash{$i} && defined $hash{$i}{$j});

[I've a sneaking feeling that a related issue was discussed recently,
but your program still leaks on 5.005_61]

BTW if you don't want to iterate over %hash (i.e. if you never use
'keys', 'each' or slices on %hash or its elements) then you may find
that using 'multidimensional hash emulation' (see $SUBSEP in perlvar(1))
is better​:
  $hash{ 10, 5 } = 1;

Ian
--
  I am confident this explanation will dispell any feelings
  of certainty that may have been troubling you.
  - BWHOLMES@​SJSUVM1.sjsu.edu (Cabbage) in <9601221753.AA27669@​spock>

@p5pRT
Copy link
Author

p5pRT commented Aug 24, 1999

From [Unknown Contact. See original ticket]

Ian Phillipps <ian@​dial.pipex.com> writes​:

This doesn't leak memory​:

print "Yeah\! $i\\n" if \(defined $hash\{$i\} && defined $hash\{$i\}\{$j\}\);

[I've a sneaking feeling that a related issue was discussed recently,
but your program still leaks on 5.005_61]

The related issue was that _60 deprecated 'defined $hash{$i}' so
that the workround above solicited a warning.

--
Nick Ing-Simmons

@p5pRT
Copy link
Author

p5pRT commented Aug 24, 1999

From [Unknown Contact. See original ticket]

Nick Ing-Simmons <nick@​ing-simmons.net> wrote

The related issue was that _60 deprecated 'defined $hash{$i}' so
that the workround above solicited a warning.

I hope not! defined() is a reasonable operation on any scalar
expression.

I thought the discussion related to defined(@​array) and defined(%hash),
which is a separate subject from this thread.

Mike Guy

@p5pRT
Copy link
Author

p5pRT commented Aug 24, 1999

From [Unknown Contact. See original ticket]

On Tue, 24 Aug 1999 at 14​:52​:16 +0000, Nick Ing-Simmons wrote​:

Ian Phillipps <ian@​dial.pipex.com> writes​:

This doesn't leak memory​:
print "Yeah! $i\n" if (defined $hash{$i} && defined $hash{$i}{$j});

[I've a sneaking feeling that a related issue was discussed recently,
but your program still leaks on 5.005_61]

The related issue was that _60 deprecated 'defined $hash{$i}' so
that the workround above solicited a warning.

I don't think that's right - $hash{$i} is a perfectly ordinary scalar,
which can be defined, undefined or nonexistent; in this example it's
probably going to be a ref(HASH) or nonexistent. Anyhow, my code
doesn't give a warning under 5.005_61. About 3.73 iotas of efficiency
could be got by saying​:
  print "Yeah! $i\n" if (exists $hash{$i} && defined $hash{$i}{$j});

Now you remind me, wasn't it defined(%hash) that was deprecated, for
local hashes? Quick test - yes​:

% perl5.00561 -we 'my %hash; defined %hash'
defined(%hash) is deprecated at -e line 1.
(Maybe you should just omit the defined()?)
Useless use of defined operator in void context at -e line 1.

Ian

@p5pRT
Copy link
Author

p5pRT commented Aug 25, 1999

From [Unknown Contact. See original ticket]

Nick Ing-Simmons <nick@​ing-simmons.net> writes​:

Ian Phillipps <ian@​dial.pipex.com> writes​:

This doesn't leak memory​:

print "Yeah\! $i\\n" if \(defined $hash\{$i\} && defined $hash\{$i\}\{$j\}\);

[I've a sneaking feeling that a related issue was discussed recently,
but your program still leaks on 5.005_61]

The related issue was that _60 deprecated 'defined $hash{$i}' so
that the workround above solicited a warning.

Ignore me - I meant defined(%hash) was deprecated...

--
Nick Ing-Simmons

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant