Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^2: Perl Recursion

by aaron_baugher (Curate)
on Sep 27, 2011 at 22:33 UTC ( #928199=note: print w/replies, xml ) Need Help??

in reply to Re: Perl Recursion
in thread Perl Recursion

First thing I noticed: there's no need to slurp the entire file into an array if you're going to loop through it line-by-line anyway, so this:

open(DAT,'<',"./")||die "Canot open file\n"; my @file=<DAT>; close DAT; shift @file;##remove header foreach my $line(@file){

would be better:

open(DAT,'<',"./")||die "Canot open file\n"; <DAT>; # discard a line while(my $line = <DAT>){ #loop through the rest

As for the rest: Ok, you're looping through the file, and for each line, you call inhibited_rec(), which calls awk to loop through the same file again, looking for a match on the first fields, then for each line, if the second field isn't 'Activation', you call activated_rec() which uses awk to loop through the same file again, perhaps recursively..... Ouch. It seems to me you could end up running your file through that pipeline at least as many times as the number of lines in the file squared, if not cubed or more.

Loop through the file once, and save the data into a structure that'll allow you to compare fields as you like. Perhaps an array of arrays, or a hash of hashes, or whatever makes sense for your data. If necessary, make a couple copies of it in different variables, so you can compare them to each other to find your circular connections (maybe not necessary, but might make it easier to understand what you're doing).

Replies are listed 'Best First'.
Re^3: Perl Recursion
by azheid (Sexton) on Sep 28, 2011 at 02:10 UTC
    You are absolutely correct. In fact, an array of arrays was exactly how I started. However, since the size of the file is not large (and my computer is really awesome), the additional time required to code such a data structure would not be worth it, at least for someone who codes on the side like me. Yes, I coped out, I admit it. :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://928199]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2017-02-28 17:12 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (406 votes). Check out past polls.