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:
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,
|