Please Check my Code...
Why isn't this code working? It's supposed to delete a certain line from a file:
$file = '$data_path/categories.txt';
$line = $FORM{'category'};
$line--;
open(IN,"<$file");
@array = <IN>;
close(IN);
splice(@array,$line,0);
open(IN,">$file");
print IN @array;
close(IN);
--Edge
Orpheus posted this at 21:14 — 24th August 2000.
They have: 568 posts
Joined: Nov 1999
your splice syntax is wrong
$start = 2;
$lines = 1;
splice(@array,$start,$lines);
$start is the number of the line it will start deleting, and $lines is the number of lines it will delete
so this example would delete line 2.
Edge posted this at 21:27 — 24th August 2000.
They have: 117 posts
Joined: Mar 2000
I changed that line to:
splice(@array,$line,1);
Which is what I had it at before, but, it still won't work.
--Edge
Orpheus posted this at 00:13 — 25th August 2000.
They have: 568 posts
Joined: Nov 1999
Try this, i havn't tested it yet though
$file = '$data_path/categories.txt';
$line = $FORM{'category'};
open(IN,"<$file");
@array = ;
close(IN);
$num = 0;
for (@array) {
if($_ =~ /$line/) {
splice(@array,$line,0);
}
$num++;
}
open(IN,">$file");
print IN @array;
close(IN);
Orpheus posted this at 00:15 — 25th August 2000.
They have: 568 posts
Joined: Nov 1999
for some reason I cant edit that post above... so here is the code that will work
$file = '$data_path/categories.txt';
$line = $FORM{'category'};
open(IN,"<$file");
@array = ;
close(IN);
$num = 0;
for (@array) {
if($_ =~ /$line/) {
splice(@array,$num,1);
}
$num++;
}
open(IN,">$file");
print IN @array;
close(IN);
Edge posted this at 00:39 — 25th August 2000.
They have: 117 posts
Joined: Mar 2000
Nope, that didn't work either. Any other ideas?
--Edge
Mark Hensler posted this at 00:50 — 25th August 2000.
He has: 4,048 posts
Joined: Aug 2000
try locating the error like this....
$file = '$data_path/categories.txt';
print $file;
$line = $FORM{'category'};
print $line;
$line--;
open(IN,"<$file");
@array = <IN>;
close(IN);
splice(@array,$line,0);
open(IN,">$file");
print @array;
print IN @array;
close(IN);
this will print all the information to the screen. maybe one of the variables being passed to the CGI is wrong. maybe you have to chomp($line).
Mark Hensler
If there is no answer on Google, then there is no question.
Mark Hensler posted this at 00:55 — 25th August 2000.
He has: 4,048 posts
Joined: Aug 2000
Orpheus-
why are you incrementing $num?
Orpheus posted this at 02:29 — 25th August 2000.
They have: 568 posts
Joined: Nov 1999
because splice requires a line to start with. or it wont work.
Edge posted this at 04:07 — 25th August 2000.
They have: 117 posts
Joined: Mar 2000
Max, your idea didn't show me anything new.
Orpheus, when I tell it to print "$num"; it always tells me $num is 0. So, I don't think that's doing any good.
--Edge
Mark Hensler posted this at 05:12 — 25th August 2000.
He has: 4,048 posts
Joined: Aug 2000
Orpheus-
I didn't see 'splice(@array,$num,1);' before..
sorry.
Edge-
did my thingy print the value for $line?
I just thought that the value from $FORM{'category'} might be formated wrong or non-numeric or something.
question...
would it make a difference if you changed it to:
@array = splice(@array,$line,0);
Mark Hensler
If there is no answer on Google, then there is no question.
Edge posted this at 15:07 — 25th August 2000.
They have: 117 posts
Joined: Mar 2000
Max-
I take that back, it did printing something:
$data_path/categories.txt3
And, nope, changing it to @array = splice(@array,$line,0); didn't make a difference.
Would it help you guys if I posted the other subroutines related to this one? Or maybe if I showed you my entire script?
--Edge
Orpheus posted this at 16:49 — 25th August 2000.
They have: 568 posts
Joined: Nov 1999
Just post the code that intereacts with that block you posted above. The stuff I pasted SHOULD work because i've used it in a guestbook before.
Edge posted this at 17:27 — 25th August 2000.
They have: 117 posts
Joined: Mar 2000
Okay, here are the subs that interact with it:
sub delcat {
open(DATA, "$data_path/categories.txt");
if ($flock eq "y") {
flock DATA, 2;
}
@data = <DATA>;
close(DATA);
foreach $line (@data) {
($catnum, $catname, $blabla) = split(/\|/, $line);
if ($catnum eq $FORM{'category'}) { last }
}
print <<TAGS;
<FORM ACTION="admin.cgi" METHOD="post">
<INPUT TYPE="hidden" NAME="action" VALUE="delete">
<INPUT TYPE="hidden" NAME="delete" VALUE="category">
<INPUT TYPE="hidden" NAME="category" VALUE="$FORM{'category'}">
<INPUT TYPE="hidden" NAME="password" VALUE="$FORM{'password'}">
<TABLE WIDTH="100%" CELLPADDING="3" CELLSPACING="1" BORDER="0">
<TR>
<TD COLSPAN="2" BGCOLOR="#e8e8e8"> <FONT SIZE="2" FACE="Verdana"> <B>Delete
Category</B></FONT></TD>
</TR>
<TR>
<TD COLSPAN="2" BGCOLOR="#f8f8f8" ALIGN="LEFT"><FONT SIZE="2" FACE="Verdana"><B>Are you sure you want to delete $catname?</B></FONT><BR>
<CENTER>
<INPUT TYPE="submit" VALUE=" Yes ">
</CENTER>
</TD>
</TR>
<TR>
</TR>
</TABLE>
</FORM>
TAGS
}
sub viewcats {
open(DATA, "$data_path/categories.txt");
if ($flock eq "y") {
flock DATA, 2;
}
@data = <DATA>;
close(DATA);
print <<TAGS;
<TABLE WIDTH="100%" CELLPADDING="3" CELLSPACING="1" BORDER="0">
<TR>
<TD COLSPAN="2" BGCOLOR="#e8e8e8"> <FONT SIZE="2" FACE="Verdana">
<B>Categories</B></FONT></TD>
</TR>
<TR>
</TR>
<TR>
<TD COLSPAN="2"><TABLE WIDTH="100%" BORDER="0" CELLPADDING="3" CELLSPACING="1">
TAGS
foreach $line (@data) {
($catnum, $catname, $blabla) = split(/\|/, $line);
print <<TAGS;
<TR BGCOLOR="#f8f8f8">
<TD WIDTH="50%" VALIGN="TOP" ALIGN="LEFT"><FONT SIZE="2" FACE="Verdana"><B>$catname</B></FONT></TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"><FORM ACTION="admin.cgi" METHOD="post">
<INPUT TYPE="hidden" NAME="action" VALUE="viewcoupons"><INPUT TYPE="hidden" NAME="category" VALUE="$catnum"><INPUT TYPE="hidden" NAME="password" VALUE="$FORM{'password'}"><INPUT TYPE="SUBMIT" VALUE="View">
</FORM>
</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"><FORM ACTION="admin.cgi" METHOD="post">
<INPUT TYPE="hidden" NAME="action" VALUE="edit"><INPUT TYPE="hidden" NAME="category" VALUE="$catnum"><INPUT TYPE="hidden" NAME="password" VALUE="$FORM{'password'}"><INPUT TYPE="SUBMIT" VALUE="Edit">
</FORM>
</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"><FORM ACTION="admin.cgi" METHOD="post">
<INPUT TYPE="hidden" NAME="action" VALUE="delcat"><INPUT TYPE="hidden" NAME="category" VALUE="$catnum"><INPUT TYPE="hidden" NAME="password" VALUE="$FORM{'password'}"><INPUT TYPE="SUBMIT" VALUE="Delete">
</FORM>
</TD>
</TR>
TAGS
}
print <<TAGS;
</TABLE>
</TD>
</TR>
</TABLE>
TAGS
}
--Edge
Mark Hensler posted this at 18:36 — 25th August 2000.
He has: 4,048 posts
Joined: Aug 2000
one thing that was wrong... In your original code you had:
$file = '$data_path/categories.txt';
it should be:
$file = "$data_path/categories.txt";
question... What does this do:
if ($catnum eq $FORM{'category'}) { last }
Mark Hensler
If there is no answer on Google, then there is no question.
Edge posted this at 23:27 — 25th August 2000.
They have: 117 posts
Joined: Mar 2000
As far as I know, it does the same thing as doing it this way:
sub delcat {
open(DATA, "$data_path/categories.txt");
if ($flock eq "y") {
flock DATA, 2;
}
@data = <DATA>;
close(DATA);
foreach $line (@data) {
($catnum, $catname, $blabla) = split(/\|/, $line);
if ($catnum eq $FORM{'category'}) {
print <<TAGS;
<FORM ACTION="admin.cgi" METHOD="post">
<INPUT TYPE="hidden" NAME="action" VALUE="delete">
<INPUT TYPE="hidden" NAME="delete" VALUE="category">
<INPUT TYPE="hidden" NAME="category" VALUE="$FORM{'category'}">
<INPUT TYPE="hidden" NAME="password" VALUE="$FORM{'password'}">
<TABLE WIDTH="100%" CELLPADDING="3" CELLSPACING="1" BORDER="0">
<TR>
<TD COLSPAN="2" BGCOLOR="#e8e8e8"> <FONT SIZE="2" FACE="Verdana"> <B>Delete
Category</B></FONT></TD>
</TR>
<TR>
<TD COLSPAN="2" BGCOLOR="#f8f8f8" ALIGN="LEFT"><FONT SIZE="2" FACE="Verdana"><B>Are you sure you want to delete $catname?</B></FONT><BR>
<CENTER>
<INPUT TYPE="submit" VALUE=" Yes ">
</CENTER>
</TD>
</TR>
<TR>
</TR>
</TABLE>
</FORM>
TAGS
}
}
}
Maybe someone else can offer a better explanation.
--Edge
Edge posted this at 00:47 — 26th August 2000.
They have: 117 posts
Joined: Mar 2000
Great! Now we're making some progress. I did what you told me and changed it to:
$file = "$data_path/categories.txt";
Then, it deleted every file EXCEPT the one I was trying to delete. Then I changed it from:
@array = splice(@array,$line,1);
to:
@array = splice(@array,$line,0);
Then it deleted all the lines. Before, it wouldn't do anything.
--Edge
Orpheus posted this at 06:03 — 26th August 2000.
They have: 568 posts
Joined: Nov 1999
Use the code I posted earlier in the thread.
splice DOES NOT use pattern matching when it deletes.
Mark Hensler posted this at 06:24 — 26th August 2000.
He has: 4,048 posts
Joined: Aug 2000
try something other than splice...
$file = '$data_path/categories.txt';
$line = $FORM{'category'};
$line--;
open(IN,"<$file");
@array = <IN>;
close(IN);
$count = "0";
foreach $thing (@array) {
unless ($count == $line) {
push(@New, $thing);
}
$count++;
}
open(IN,">$file");
print IN @New;
close(IN);
Mark Hensler
If there is no answer on Google, then there is no question.
Mark Hensler posted this at 06:26 — 26th August 2000.
He has: 4,048 posts
Joined: Aug 2000
I copied and pasted from the first post, and forgot to change ' to "
also, this is kinda sloppy with the memory, if you can get the splice to work, that would be better.
Mark Hensler
If there is no answer on Google, then there is no question.
Edge posted this at 15:20 — 26th August 2000.
They have: 117 posts
Joined: Mar 2000
Your code worked, Orpheus! Thanks!!!!!! Sorry it took so long to figure out such a simple problem, guys. Thanks to both of your for your help!
--Edge
Orpheus posted this at 18:03 — 26th August 2000.
They have: 568 posts
Joined: Nov 1999
*bows*
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.