Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printing till blank line

Status
Not open for further replies.

bhatpp

Programmer
Joined
Jun 7, 2007
Messages
3
Location
IN
Hi All,
I am new to perl and facing an interesting situation.
There are 2 conditions to be matched in my code.
1. If the first condition is matched, then the 3 lines following that condition should be printed.
2. If the second condition is matched, then the 12-15 lines below that must be printed.

For the first case its working fine. But for the second case, it is printing only 4 lines.
Here is the extract of that code::

@fcontent=<IN>;

while(@fcontent){

$line = shift(@fcontent);

if($line =~ m/<pattern1>/)
{
(@line)= split(' ', $line);
$len=@line;
$i=0;

while ($i <= $len) {

print OUT $line;
$line = shift(@fcontent);

$i++;

}
print OUT "\n";
}
if($line =~ m/<pattern2>/)
{
(@line)= split(' ', $line);
$len=@line;
$i=0;

while ($i <= $len) {

print OUT $line;
$line = shift(@fcontent);

$i++;

}
print OUT "\n";
}
}
 
The number of lines you are really printing corresponds to the number of space separated words in the line where the condition is met (plus the line with the condition itself): so to judge one should know the content of your file.
Note also that an elsif in place of the if for the second condition would in principle be preferred to avoid that condition 2 is possibly fired by the last line printed after condition 1 was met.
There are other efficiency and cleanliness of writing issues with your code, but to me it appears to do what you stated, except as noted above.

 
Yes, prex1's analysis is correct.

The natural thing to ask and try if you're code is not behaving as expected would be to print debugging information. Is $len the value that you expect given the line? Only you can answer that.

Anyway, here's your code a little cleaned up. I suggest that you always "use strict;".

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[olive][b]while[/b][/olive] [red]([/red]<IN>[red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$line[/blue] = [blue]$_[/blue][red];[/red]

	[olive][b]if[/b][/olive] [red]([/red][blue]$line[/blue] =~ [red]m/[/red][purple]<pattern1>[/purple][red]/[/red][red])[/red] [red]{[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] OUT [blue]$line[/blue][red];[/red]

		[black][b]my[/b][/black] [blue]$len[/blue] = [url=http://perldoc.perl.org/functions/scalar.html][black][b]scalar[/b][/black][/url][red]([/red][url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url] [red]'[/red][purple] [/purple][red]'[/red], [blue]$line[/blue][red])[/red][red];[/red]

		[olive][b]for[/b][/olive] [red]([/red][fuchsia]1..[/fuchsia][blue]$len[/blue][red])[/red] [red]{[/red]
			[blue]$line[/blue] = <IN>[red];[/red]
			[black][b]print[/b][/black] OUT [blue]$line[/blue][red];[/red]
		[red]}[/red]
		
		[black][b]print[/b][/black] OUT [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

	[red]}[/red] [olive][b]elsif[/b][/olive] [red]([/red][blue]$line[/blue] =~ [red]m/[/red][purple]<pattern2>[/purple][red]/[/red][red])[/red] [red]{[/red]
		[black][b]print[/b][/black] OUT [blue]$line[/blue][red];[/red]

		[black][b]my[/b][/black] [blue]$len[/blue] = [black][b]scalar[/b][/black][red]([/red][black][b]split[/b][/black] [red]'[/red][purple] [/purple][red]'[/red], [blue]$line[/blue][red])[/red][red];[/red]

		[olive][b]for[/b][/olive] [red]([/red][fuchsia]1..[/fuchsia][blue]$len[/blue][red])[/red] [red]{[/red]
			[blue]$line[/blue] = <IN>[red];[/red]
			[black][b]print[/b][/black] OUT [blue]$line[/blue][red];[/red]
		[red]}[/red]
		
		[black][b]print[/b][/black] OUT [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[red]}[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
[/tt]

- Miller
 
OK, I got you.. Yes.. Actually, $len has to have the value of number of lines before the blank line.. Then print that many number of lines..
Can anyone suggest me what I should be doing to print 12-15 lines below a particular pattern..
I mean can anyone give the approach?
 
maybe:

Code:
@fcontent=<IN>;

foreach my $i (0..$#fcontent) {
   if ($fcontent[$i] =~ /^\s*/) {
      print $_ for @fcontent[$i .. $i+12];
   }
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
this:

if ($fcontent[$i] =~ /^\s*/) {

should be:

if ($fcontent[$i] =~ /^\s*[red]$[/red]/) {

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Actually kevin, the code which u suggested is printing the same sequence repeated number of times..
I also need to check for two conditions, based on which, lines following them has to be printed.. See my initial mail..
 
in theory it works:

Code:
@array = ("blah", " ", 1..20);
foreach my $i (0..$#array) {
   if ($array[$i] =~ /^\s*$/) {
      print "$_\n" for @array[$i..$i+12];
   }
}

output:

1
2
3
4
5
6
7
8
9
10
11
12



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
bhatpp,
if that number of lines is always the same, just set $len to that value in the code that KevinADC provided, instead of determining $len from the content of the file.
If the number of lines is variable, you should specify the rule to determine it, before coding anything.

 
Under those circumstances, wouldn't
Code:
use strict;
use warnings;

while (<>) {
   print if (/<pattern1>|<pattern2>/ .. /^\s*$/);
}
have the same effect, without reading the file into an array or using Tie::File? (untested)...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top