qingxia has asked for the wisdom of the Perl Monks concerning the following question:
I have following while using nextline. Bascially, i am parsing some string(N here) in the second line after matching the target. e.g.
And i apply this to find a couple of variables. The code i have at hand is:(line1) target some text here (line2) some more text here N
#!/usr/bin/perl print "Borrower\tactiveDate\tAmount\n"; $Borrower = "null"; $activeDate = "null"; $Amount = "null"; $on = 0; open(FILE, $ARGV[0]); while ($line = <FILE>) { if ($on eq 0 and $line =~ /<tr/) { $on = 1; } if ($on eq 1) { if ($line =~ /<div(.*?)>Borrower:<\/td>/) { $nextline = <FILE>; if ($nextline =~ /<td(.*?)>(.*?)(\s)/) { $Borrower = $2;} if ($nextline =~ /<td(.*?)>(.*?)<\/td>/) { $Borrower = $2;} } if ($line =~ /(\s)active(\s)date\:<\/td>/) { $nextline = <FILE>; if ($nextline =~ /<td(.*?)>(.*?)<\/td>/) { $activeDate = $2;} } if ($line =~ /<td(.*?)>Amount:<\/td>/) { $nextline = <FILE>; if ($nextline =~ /<td(.*?)>(.*?)(\s)/) { $Amount = $2;} if ($nextline =~ /<td(.*?)>(.*?)<\/td>/) { $Amount = $2;} } } if ($line =~ /<\/tr>/) { $on = 0; if ($Borrower ne "null") { print "$Borrower\t$activeDate\t$Amount\n"; $Borrower = "null"; $activeDate = "null"; $Amount = "null"; } } } close(FILE);
the code goes well for the first variable namely 'borrower', however it starts going wrong from the rest. It is 'null' for the first 'borrower' in the variables 'activedate' and 'Amount', and one position lagged matched for the rest. i.e. the right 'activedate' and 'Amount' value of first borrower goes to the second borrower, the second goes to the third, so on and so forth.
Stuck for a while, hope someone help me out of here. thanks a lot in advance. regards,
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: $nextline not working
by davido (Cardinal) on Mar 22, 2013 at 04:17 UTC | |
|
Re: $nextline not working
by McA (Priest) on Mar 22, 2013 at 03:00 UTC | |
|
Re: $nextline not working
by McA (Priest) on Mar 22, 2013 at 03:54 UTC | |
|
Re: $nextline not working
by farang (Chaplain) on Mar 22, 2013 at 17:16 UTC |