I think ur notation is what's confusing u.  I don't think it's making a copy
but it would be better to write the $num construct as:

$nums->{$i} = $numbernames[$i];


HTH.



At 09:51 AM 3/2/2011 -0800, Barry Brevik wrote:
>I have a subroutine to which I want to pass a hash by reference. In the
>real program, the hash in the caller starts out empty, and the
>subroutine adds values to it.
> 
>The hash eventually becomes quite large, so I want the subroutine to add
>values to the caller's hash. The following sample code does this, BUT I
>have a concern that the subroutine might be making a local copy of the
>caller's hash.
>
>The code below shows how I am doing it now. It appears to work as
>intended, but how can I ensure that the sub is not making a local copy
>of the hash??
>
>Thanks in advance:
>
>use strict;
>use warnings;
>
>my %numbers = ();
>
>makeNumbers(\%numbers);
>
>foreach my $key (keys(%numbers))
>{
>  print "$key  $numbers{$key}\n";
>}
>
>#-------------------------------
>sub makeNumbers
>{
>  my @numbernames = qw(zero one two three four five);
>
>  if (my $nums = shift)
>  {
>    for (my $i = 0; $i < 6; $i++)
>    {
>      ${%{$nums}}{$i} = $numbernames[$i];
>    }
>  }
>}
>


--
REMEMBER THE WORLD TRADE CENTER         ---=< WTC 911 >=--
"...ne cede malis"

00000100

_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to