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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl newbie help with regex

Status
Not open for further replies.
Jun 3, 2007
84
US
Hello I have the following code in my script that does not seem to work and I can figure out why it's not working. Hoping that someone could please help me out.


open(HAN2, "/home/testuser01/test.txt") || die "Count not open test.txt file";
while (<HAN2>) {
@array1=[ split ];
foreach (@array1) {
print @$_, "\n"; # Testing purposes
if (@$_ =~ /^\s+$/) {
print "found", "@$_", "\n";
}
}
}


The file test.txt contains
blank/null line
aaaaaa
bbbbbb
cccccc

So when I print I would like it to only print what is shown below and not the blank line.
aaaaa
bbbbb
ccccc

But it prints
null/blank line along with
aaaaa
bbbbb
ccccc

In reality what I am trying to do is not print any lines in the array which it reads from a file, which are blank. I have a file which sometimes has blank lines but don't want those to be printed. I am sure there is an easier way to do this and I would love to learn them, but I would also love to know what I am doing wrong/why the print is printing the blank line along with the lines which contain data.

thanks for the help in advance.
 
Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$file[/blue] = [red]"[/red][purple]/home/testuser01/test.txt[/purple][red]"[/red]

[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red][black][b]my[/b][/black] [blue]$fh[/blue], [blue]$file[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$file[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[olive][b]while[/b][/olive] [red]([/red]<[blue]$fh[/blue]>[red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
	[olive][b]next[/b][/olive] [olive][b]if[/b][/olive] [red]/[/red][purple]^[purple][b]\s[/b][/purple]*$[/purple][red]/[/red][red];[/red] [gray][i]# Skip blank lines[/i][/gray]
	
	[black][b]my[/b][/black] [blue]@array1[/blue] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red];[/red]

	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]@array[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
[/tt]
 
Thanks for the help!!
that did it, it works now.

thanks again for the quick help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top