PDA

View Full Version : Why doesnt this code work?



gazz
November 29th, 2000, 19:29
Im writting a script that first displays all the poem files in a directory by their title inside the file. Each file starts off by the poem title and has a pipe '|' after it to parse with. The script is suppost to first come up with a list of all the poem titles gotten from the files. It links to call itself to display the poem once clicked.
The script shoes the files up top so you can compare what its showing to what is actually.
Heres the script uploaded and note carefully what it does: http://www.arcnemisis.f2s.com/playstuff/showdir.cgi

Heres the main part:


if($ENV{'QUERY_STRING'} eq '') {
### display poem titles.

opendir(DIR, ".");
@files = sort(grep(/shtml$/, readdir(DIR)));
closedir(DIR);

$FilesFound = @files;
foreach (@files)
{
print(" <a href='$_'> $_ </a> ");
}

print<<EOF;
<table width="600" bgcolor="FFFFFF" border="0">
<tr><td>
EOF

foreach (@files) {
@POEM = NULL;
$TEMP = $_;
open(FILE,"<$TEMP") or goterror("Unable to read $TEMP.");
while(<FILE>) {
@POEM = split(/\|/, $_);
}
close(FILE);
print("<br><a href='showdir.cgi?$TEMP'>$POEM[0]</a><br>");
}

print<<EOF;
</td></tr></table><br>
EOF

} #Endif

else {
### Display particular poem.
$FILENAME = $ENV{'QUERY_STRING'};
print("The file is $FILENAME");


print<<EOF;
<table width="600" bgcolor="FFFFFF" border="0">
<tr><td>
EOF

open(FILE,"<$FILENAME") or goterror("Unable to read $FILENAME.");
while(<FILE>) {
@POEM = split(/\|/, $_);
}
close(FILE);
print($POEM[0]);
print($POEM[1]);

print<<EOF;
</td></tr></table><br>
EOF

} #endelse



Someone please tell me why its not doing as it should.. ive tried for hours and I can spot it.

Thanks.

gazz
November 30th, 2000, 16:33
Doesnt anybody have a clue??

perlboy
November 30th, 2000, 17:32
MMMMM... hard one. First of all stop confusion by naming all variable, yes it is more efficient to use $_ but it doesn't help with readability.

>foreach (@files)
>{
>print(" <a href='$_'> $_ </a> ");
>}

Why is this part here? You are trying to display the titles not the filenames. Kill that.

>print<<EOF;
><table width="600" bgcolor="FFFFFF" border="0">
><tr><td>
>EOF

This is what I would write;

foreach $file (@files) {
open(FILE,"<$file") or goterror("Unable to read $file.");
$file_source = <FILE>;
close(FILE);
($filetitle, $rubbish) = split(/\|/,$file_source);
print "<br><a href='showdir.cgi?$file'>$filetitle</a><br>";
}

print<<EOF;
</td></tr></table><br>
EOF

} #Endif

Try that.

gazz
December 1st, 2000, 14:14
Ok great, the first part works. I dont userstand why it works and mine didnt but it does.

now the else statement:

else {
### Display particular poem.
$FILENAME = $ENV{'QUERY_STRING'};
print<<EOF;
<table width="600" bgcolor="FFFFFF" border="0">
<tr><td>
EOF
open(FILE,"<$FILENAME") or goterror("Unable to read $file.");
$file_source = <FILE>;
close(FILE);
print($file_source);
$file_source =~ tr/|/ /;
print("$file_source<br>");

print<<EOF;
</td></tr></table><br>
EOF
} #endelse


I have the two print $file_source statements in there just to see whats hapening. The first one will print the title out up to the pipe then stop. After i try the replace on filesource i just get the title without the pipe. I want the whole poem without the pipe printed.

(Im having a horrid week here)
Thanks for the help!!