Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Actually I have to run the above code recursively to get the result: So here is my complete code:
#!/usr/bin/perl -w use diagnostics; use warnings; open $fh, "<", "$ARGV[0]" or die "Could not open $ARGV[0]: $!"; sub getsub{ my $sub = $_[0]; print "inside sub for $sub\n"; seek $fh, 0, 0; while (<$fh>) { if (/\.subckt $sub/../\.ends/) { print "starting from $_\n"; if (/^x/) { $line = $_; while (($nxt = readline($fh)) =~ /^\+/) { $line = $nxt; # print "line changed to $line\n"; } $line =~ s/\s+$//; # print "last line is $line\n"; my $sub = (split '\s', $line)[-1]; print "subcircuit found is $sub in $line\n"; my $pos = tell($fh); print "position in file set to $pos\n"; getsub ($sub); seek $fh, $pos, 0; $pos = tell ($fh); print "position in file restored to $pos\n"; print "line is now $_"; } } } } while (<$fh>) { if ($_ =~ /^xa1/) { $line = $_; print "line found to be $line\n"; while (($nxt = readline($fh)) =~ /^\+/) { $line = $nxt; #print "line changed to $line\n"; } $line =~ s/\s+$//; # print "last line is $line\n"; my $sub = (split '\s', $line)[-1]; print "subcircuit found is $sub in $line\n"; getsub($sub); } }
The file on which I am running the code is too big to be shared. The problem I am getting is inside the subroutine. What I want is whenever I return from the recursive call from the subroutine I want to start from the position where I left off in that routine. So I saved the position in $pos and used that in seek after the recursive call as suggested by you. When I return back from a recursive call I tried to check the line which I am on using the print statement at the end of the subroutine. But it shows $_ to be blank. Also, the if block starts from the beginning again creating an infinite loop. Why is this happening?

In reply to Re^2: Variable not set properly in perl by sar123
in thread Variable not set properly in perl by sar123

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 13:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found