Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hello again sar123,

I agree with Anonymous Monk below that it would be much more efficient to read the input file once, storing its contents into a hash or other suitable data structure, and then to access the data from memory, than it is to re-read the file each time getsub is called. But the following observations refer to the strategy you have adopted:

Why do you need sub getsub to call itself recursively? Your code as shown violates the DRY principle of software development, by repeating code from the main while loop in the subroutine. Here’s my guess as to what’s going on: if the input file contains successive lines like this:

xa1 b t g e1 xa1 a s f a1

the second is ignored. But the reason for this is the logic of the code beginning:

while (($nxt = readline($fh)) =~ /^\+/) {

and the solution is not recursion, but re-casting the logic:

Input data file “subs.txt”:

.subckt a1 x y z xa a b c1 xb c d e1 xc f g h1 .ends .subckt c1 x y z xa a b f xb c d e xc f g h .ends .subckt e1 x y z xa a b c1 xb c d k1 xc f g h1 .ends xa1 b t g e1 xa1 a s f a1

Script:

use strict; use warnings; open my $fh, '<', $ARGV[0] or die "Cannot open file '$ARGV[0]' for reading: $!"; sub getsub { my ($sub) = @_; print "inside sub for $sub\n"; seek $fh, 0, 0; (/\.subckt $sub/ .. /\.ends/) && print while <$fh>; } my $line = <$fh>; while ($line) { if ($line =~ /^xa1/) { print "line found to be $line\n"; my $next; $line = $next while !eof && ($next = <$fh>) =~ /^\+/; $line =~ s/\s+$//; my $sub = (split /\s+/, $line)[-1]; print "subcircuit found is $sub in $line\n"; my $file_pos = tell $fh; getsub($sub); seek $fh, $file_pos, 0; $line = $next; } else { $line = <$fh>; } } close $fh or die "Cannot close file '$ARGV[0]': $!";

Output:

13:20 >perl 1032a_SoPW.pl subs.txt line found to be xa1 b t g e1 subcircuit found is e1 in xa1 b t g e1 inside sub for e1 .subckt e1 x y z xa a b c1 xb c d k1 xc f g h1 .ends line found to be xa1 a s f a1 subcircuit found is a1 in xa1 a s f a1 inside sub for a1 .subckt a1 x y z xa a b c1 xb c d e1 xc f g h1 .ends 13:20 >

If this doesn’t solve your problem, then as frozenwithjoy says you will need to provide more information, including a minimal but complete example script together with sample input and desired output.

By the way, you really should get into the habit of coding with:

use strict;

— this will save you a lot of debugging time down the track. And note that:

#!/usr/bin/perl -w ... use warnings;

is redundant; use warnings; is generally preferred over -w because the former is lexically scoped, whereas the latter has global effect.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^3: Variable not set properly in perl by Athanasius
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 sharing their wisdom with the Monastery: (6)
As of 2024-03-28 08:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found