CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Recursion and "local" variables.

 



DrZed
User

Jul 3, 2000, 7:42 PM

Post #1 of 3 (363 views)
Recursion and "local" variables. Can't Post

I've tried to use a local variable in a recursive routine, but I couldn't get it to work. I understand non-recursive uses for local, but I'm wondering if there's a better way to do what I want in a recursive routine.

What I want is a single variable (a hash) which can be accessed by the routine during multiple recursions.

What I'm using is something like the following:

&recuse($data);

sub recurse
{
my (%main_hash);
my ($data,$hash_ref) = (shift,shift | | \%main_hash);
....
&recurse($data,$hash_ref);
....
}

All manipulation of the 'main hash' is done through $hash_ref. So, the first call of the routine will assign $hash_ref to it's %main_hash and the reference will be passed to each recursion.

It works, but it seems a bit sloppy, since each recursion will be allocating space for a %main_hash that won't be used.

I'm just curious if there's a better way of doing this.

Dr. Zed




Kanji
User / Moderator

Jul 3, 2000, 8:18 PM

Post #2 of 3 (363 views)
Re: Recursion and "local" variables. [In reply to] Can't Post

Define your hash and the sub definition in the same block so they share the same scope.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

#!/usr/bin/perl -w

my $data = 0;
recurse( $data );

{
my %hash;

sub recurse {
my $data = shift;
$data++;

print "%hash contains ",
( %hash ? join( ", " => keys %hash ) : "nothing" ),
"\n";

$hash{ $data } = 1;

recurse( $data ) unless keys %hash == 10;
}
}
__END__
%hash contains nothing
%hash contains 1
%hash contains 1, 2
%hash contains 1, 2, 3
%hash contains 1, 2, 3, 4
%hash contains 1, 2, 3, 4, 5
%hash contains 1, 2, 3, 4, 5, 6
%hash contains 7, 1, 2, 3, 4, 5, 6
%hash contains 1, 2, 3, 4, 5, 6, 7, 8
%hash contains 1, 2, 3, 4, 5, 6, 7, 8, 9</pre><HR></BLOCKQUOTE>
There is a proper name for this, but I forget offhand. (Closure?)


DrZed
User

Jul 3, 2000, 11:50 PM

Post #3 of 3 (363 views)
Re: Recursion and "local" variables. [In reply to] Can't Post

Well, that's closer to what I wanted, and I can make that work.

The only quirk is that the data will keep it's data between calls. I need something that's new to each call the first time it's called.

Hmmm.... If you make the outer parenthesis a subroutine and make the hash a local variable...


Code
#!/usr/bin/perl -w 

parse(0);
print "="x40,"\n";
parse(0);

sub parse
{local %hash;
&recurse;
sub recurse
{my $data = shift;
$data++;
print "%hash contains ",
( %hash ? join( ", " => keys %hash ) : "nothing" ),"\n";
$hash{ $data } = 1;
recurse( $data ) unless keys %hash == 10;
}
}
[\code]

That's basically what I was looking for.

Thanks.

Dr. Zed

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives