help passing %array

They have: 11 posts

Joined: Nov 1999

I'm trying to pass an associative array from sub1 to sub2. How do I make the call from sub1 and use it in sub2? I have six name/value pairs in the %array but only three seem to be displayed when I use the syntax...

sub1
{
sub2(%array}

}

sub2
{
%newarray=@_
for each $index(keys(%newarray))
print "$newarray{$index";

}

when I print the array from sub1 all values are printing fine.

Thanks,
Aaron

They have: 69 posts

Joined: Apr 1999

Try the following code:

sub1 {
&sub2({%array});
}

sub2 {
*newarray = shift;
for (keys %newarray) {
print $newarray{$_};
}
}

Haven't tested it -- should work.

------------------
[ Web-Reviews ]
http://www.web-reviews.com/

They have: 161 posts

Joined: Dec 1999

The following code has been tested (this is always a good thing to do when posting answers) and works:

sub func1 {
func2(@_);
# or &func2;
# (that would probably be faster, as it
# sends the contents of @_ by default)
}

sub func2 {
my %hash = @_;
my $key;
foreach $key (keys %hash) {
print "$key => $hash{$key}\n";
}
}

They have: 297 posts

Joined: Apr 1999

Froma perl style point of view Fred's answer is the better and much much faster solution.

You might also find a way doing this using objects.

Take a look a the perlob man page

Later,

Malte

------------------
Malte Ubl - www.Boardzilla.org
Communication: public<->programmers
of the Boardzilla BB

They have: 161 posts

Joined: Dec 1999

Malte, actually, I beg to differ, because his solution has one line that I find just as inefficient as what I did:

code:

sub1 {
  &sub2({%array});  # THIS HERE
}

sub2 {
  *newarray = shift;
  for (keys %newarray) {
    print $newarray{$_};
  }
}[/code]

I definitely agree that the typeglobbing is nice, but the line I have a problem with is your sub1() function.  By saying {%hash} instead of \%hash, you are effectively copying the elements, and for a big hash, this can be slow.  {%hash} creates a reference to an anonymous hash, whose contents are the same as %hash's, but \%hash creates a direct reference to %hash.

Also, while we're on the topic of style, perhaps the for-loop should be substituted by a while-loop, using the each() function; this is also faster, because keys %hash returns a lengthy list for big hashes.

I offer this solution:

code:
sub func1 {
  my %hash;
  # populate %hash somehow
  func2(\%hash);
}

sub func2 {
  local *hash = shift;
  my ($k,$v);
  while (($k,$v) = each %hash) {
    # do whatever
  }
}[/code]

For those interested, I've posted a benchmark online at http://www.pobox.com/~japhy/tmp/perl/bm.txt 

That's all for now.

------------------
-- 
MIDN 4/C PINYAN, NROTCURPI, USNR 

They have: 103 posts

Joined: Apr 1999

I have to agree with Jaffy here for the most part. Here is my version of it.

code:

sub1
{
my $hash = (
a => val,
b => val2,
c => val3
);
sub2(%hash);
}

sub 2
{
my %hash = @_;
do (something with %hash);
}
[/code]

You're free to go from here. What japhy did with the while loop was assign each key and value a scalar so that you could use them individually. This may not be want you want to do.




[This message has been edited by Gil (edited 14 December 1999).] 

Gil Hildebrand, Jr.
Internet Consultant
New Orleans, LA

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.