help passing %array
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
Federico Carnales posted this at 04:23 — 12th December 1999.
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/
japhy posted this at 05:14 — 12th December 1999.
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";
}
}
Malte posted this at 20:35 — 14th December 1999.
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
japhy posted this at 21:06 — 14th December 1999.
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:
Gil posted this at 03:22 — 15th December 1999.
They have: 103 posts
Joined: Apr 1999
I have to agree with Jaffy here for the most part. Here is my version of it.
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.