Gossamer Forum
Home : General : Perl Programming :

Inserting into existing pages

Quote Reply
Inserting into existing pages
Hi,

I am trying to insert stuff into the middle of an existing page but it is not working, the code is below....any help is appreciated.

open(FILE4,">>$basepath/$basedir/databoard.dat") | | die $!;

foreach $line (@lines) {
if ($line =~ /<!-- $FORM{'rnumber'} -->/) {
print FILE4 "<!-- $FORM{'rnumber'} -->\n";
print FILE4 "<ul><li>\n";
print FILE4 "<b><a href=\"$URL/$baseindex/$messages/$count.$pages\">$FORM{'nsubject'}</a></b>";

if ($newmessage eq "0") {
print FILE4 "(n/t)";
}

print FILE4 " - $months[$mon] $daym, $year\n";
print FILE4 "<!-- $count -->\n";
print FILE4 "</ul>\n";

}
}

CLOSE(FILE4);


Brett
Quote Reply
Re: Inserting into existing pages In reply to
oops....

forgot a bit of code above it...all the code is below.

open(FILE4,"$basepath/$basedir/databoard.dat") &#0124; &#0124; die $!;
@lines = <FILE4>;
close(FILE4);


open(FILE4,">>$basepath/$basedir/databoard.dat") &#0124; &#0124; die $!;

foreach $line (@lines) {
if ($line =~ /<!-- $FORM{'rnumber'} -->/) {
print FILE4 "<!-- $FORM{'rnumber'} -->\n";
print FILE4 "<ul><li>\n";
print FILE4 "<b><a href=\"$URL/$baseindex/$messages/$count.$pages\">$FORM{'nsubject'}</a></b>";

if ($newmessage eq "0") {
print FILE4 "(n/t)";
}

print FILE4 " - $months[$mon] $daym, $year\n";
print FILE4 "<!-- $count -->\n";
print FILE4 "</ul>\n";

}
}

CLOSE(FILE4);
Quote Reply
Re: Inserting into existing pages In reply to
Code:
open(RFILE4,"$basepath/$basedir/databoard.dat") | | die $!;
@lines = <RFILE4>;
close(RFILE4);
$line=0;
foreach $mline (@lines) {$line++;
if ($mline=~ /<!-- $FORM{'rnumber'} -->/i) {$line--;
if ($newmessage eq "0") {
$nonewmessage = "(n/t)";
}
# Add the following to @ databoard.dat
# Is there another way to do this, cannot tab or it prints tab
# and I don't like it all on one line with \n's
# leave it at the left margin or things will get messed up
@add=("
<!-- $FORM{'rnumber'} -->
<ul><li>
<b><a href=\"$URL/$baseindex/$messages/$count.$pages\">$FORM{'nsubject'}</a></b>
$nonewmessage
- $months[$mon] $daym, $year
<!-- $count -->
</ul>\n");
# end @add
splice (@lines, $line, 1, @add);
# update databoard.dat
open (WFILE4, ">$basepath/$basedir/databoard.dat") | | die $!;
print WFILE4 @lines;
close (WFILE4);
Quote Reply
Re: Inserting into existing pages In reply to
Hi,

Thanks for the code, but, I think u forgot to close off your foreach & if's.

Should I just chuck
}
}
down the bottom of that code?

Brett
Quote Reply
Re: Inserting into existing pages In reply to
Hi,

Well I chucked those 2 brackets at the end of the code as I was gettings errors before...

The script did'nt give any errors after I added them, but that code still does'nt insert into the page.


Brett
Quote Reply
Re: Inserting into existing pages In reply to
HI,

Also, when you set an array or scalar variable to multiple lines like this:

@lines = ("
wweeeeeeeeeee
llllll\n");

I thought u were supposed to use '&' & not "&"?


Brett
Quote Reply
Re: Inserting into existing pages In reply to
You're right I didn't close if and foreach. I don't think I messed anything else up, it is working code and not off the cuff. Are you sure <!-- $FORM{'rnumber'} --> exists in databoard.dat and that $FORM{'rnumber'} is getting passed to the script...uh oh I thought it was rnumber looks like its mumber (a 'm' not an 'r' and a 'n'), 2 occurrences to change.

If

@add=("
foo
bar\n");

bothers you try

@add="foo\nbar\n";

>"I thought u were supposed to use '&' & not "&"?"

You mean single and not double quotes?, I'm just a hacker but I seem to remember that anything containing a variable is double quoted.

[This message has been edited by Dave (edited August 12, 1999).]
Quote Reply
Re: Inserting into existing pages In reply to
HI,

um....no...u were right, it was an "r" as in rnumber.

The script gets run by clicking on form submit buttons, so $FORM{'rnumber'} refers to the form name rnumber, so, yes it does exist.

As for the single & double quotes, i know that variables have to be inside double quotes, but if you are going to extend onto multiple lines you must use sineg quotes.

I tried the other method you showed me where you put @add= on one line, but it still did'nt work.

Any suggestions?

Brett
Quote Reply
Re: Inserting into existing pages In reply to
Take a look at the Review It script which uses this method.

Wondering if the following works (if it does I'll take another look at the snippet I posted)

foreach $mline (@lines) {
if ($mline=~ /<!-- $FORM{'rnumber'} -->/i)
print "$FORM{'rnumber'}\n";
}
}

Also try using the -w switch and checking your error logs.
Quote Reply
Re: Inserting into existing pages In reply to
HI,

Well, I used the -w thing & this is what I get in my error log:

Unquoted string "newie" may clash with future reserved word at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 215.
Unquoted string "repie" may clash with future reserved word at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 218.
Name "main::add2" used only once: possible typo at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 542.
Name "main::isds" used only once: possible typo at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 111.
Name "main::add" used only once: possible typo at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 523.
Unquoted string "newie" may clash with future reserved word at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 215.
Unquoted string "repie" may clash with future reserved word at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 218.
Name "main::add2" used only once: possible typo at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 542.
Name "main::isds" used only once: possible typo at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 111.
Name "main::add" used only once: possible typo at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 523.
Use of uninitialized value at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 89.
Use of uninitialized value at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 515.
Use of uninitialized value at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 515.
Use of uninitialized value at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 515.
Use of uninitialized value at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 515.
Use of uninitialized value at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 515.
Use of uninitialized value at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 515.
Use of uninitialized value at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 515.
Use of uninitialized value at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 515.
Use of uninitialized value at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 515.
Use of uninitialized value at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 515.
Use of uninitialized value at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 533.
No such file or directory at /data1/hypermart.net/beaniesafari/bbs/wwwboard.cgi line 533.

lines 515 & 533 is around where the code is for what I am trying to fix here.


Brett