Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: Variable not set properly in perl

by Athanasius (Archbishop)
on Sep 29, 2014 at 03:32 UTC ( [id://1102314]=note: print w/replies, xml ) Need Help??


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

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,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1102314]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-20 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found