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!

Odd Loop behaviour

Status
Not open for further replies.

audiopro

Programmer
Joined
Apr 1, 2004
Messages
3,165
Location
GB
The following subroutine is extracting HTML from a text file. It works fine in other instances but in this particular case I am losing the value of $PRIFMESS.
Instance 1 - it contains data
Instance 2 - it is blank
So nothing is returned.
Am I missing the obvious?
Code:
sub create_prifmess_from_html{
	my $filename="html/".$_[0];
	my $part;
	open (LOG, "<".$filename) || &errormess(34,"Cannot open $_[0]");
	my @allmessages=<LOG>;
	close (LOG);
	my $html_on;
	my $PRIFMESS="";
	foreach $part (@allmessages){
		chomp $part;
		if($part eq "<!-- COMMENCE HTML -->"){$html_on=1}
		if($part eq "<!-- FINISH HTML -->"){$html_on=0}
		my $LENF=length($part);
		if($LENF>0){
			if($html_on == 1){$PRIFMESS.=$part}
		}
[RED]print "1 - $PRIFMESS";[/RED]
	}
[RED]print "2 - $PRIFMESS";[/RED]

	return($PRIFMESS);
}

Keith
 
here:

print "2 - $PRIFMESS";

will have the final value of $PRIFMESS and should be what is returned. Maybe the problem is in the part of the script where you call the subroutine.

Do you ever use single-quotes? There is only one line in the above code that needs double-quotes:

"Cannot open $_[0]"

all the rest of your strings should be single-quoted. Using double-quotes needlessly slows down your perl scipts as perl checks for meta characters or variables that need interpolation within double-quoted strings.
 
Thanks for the tip on quotes Kevin, I am just in the habit of using doubles. I will get someone to slap me each time I use them in future.

Not sure how the calling part could affect it.
Code:
my $PRIFMESS=&create_prifmess_from_html("index.htm");
$PRIFMESS contains data until it exits the foreach loop and then suddenly becomes empty, very odd.

Keith
 
it seems to work OK for me. Whats in your index.htm file?
 
I re wrote the whole procedure and the line which calls it and the new version worked. I could not see any differences between the two versions and even did a bit comparison between the two. They are identical yet one works, the other doesn't.
I am confused but now it is working and I must get on, thanks for your help I just wish I could say what the error was.


Keith
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top