
May 23rd, 2000, 11:05 PM
|
|
Registered User
|
|
Join Date: Dec 1999
Posts: 18
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
hello... i am working hard again trying to learn perl...
i have a script that inputs data into a file line by line... if i want to change one of those lines... the script will do that...
i have two questions...
#1 how do i delete a line...?
i have tried: undef $line (actually it reads as: undef $changes[$fields{linenumber}];
this undefines the line, but leaves me with a blank line or a n still in the file, so my output is all messed up...
i don't know how to "erase" the line completely...
#2 how do i call a subroutine using a variable...?
i have two submit buttons... "adjust" and
"delete"... when the adjust button is pushed i want to call the subroutine:
&adjust;
when the delete button is pushed:
&delete;
my variables are parsed in the form of:
$fields{key}... so i tried to call the sub by using:
&$fields{key};
this did not work... all i got was a server error...
how do i call a sub using a variable like this...?
here is that portion of the script:
######################################
# subroutine delete file
######################################
sub Delete
{
$out="cgi-bin/order/$ENV{'REMOTE_ADDR'}.txt";
open OUT, "$out" or die "Cannot open $out for write :$!";
read(OUT,$oldstuff,10000);
close OUT;
@changes=split(/n/,$oldstuff);
open OUT, ">$out" or die "Cannot open $out for write :$!";
undef $changes[$fields{linenumber}]; ### this leaves a blank line which messes up my output ###
for $x (0 .. $#changes)
{
print OUT "$changes[$x]n";
}
close OUT;
}
######################################
# subroutine adjust file
######################################
sub Adjust
{
$out="cgi-bin/order/$ENV{'REMOTE_ADDR'}.txt";
open OUT, "$out" or die "Cannot open $out for write :$!";
read(OUT,$oldstuff,10000);
close OUT;
@changes=split(/n/,$oldstuff);
open OUT, ">$out" or die "Cannot open $out for write :$!";
$changes[$fields{linenumber}]=$adjust;
for $x (0 .. $#changes)
{
print OUT "$changes[$x]n";
}
close OUT;
}
|