Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Try It Free
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old May 23rd, 2000, 11:05 PM
tucats tucats is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 1999
Posts: 18 tucats User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;

}

Reply With Quote
  #2  
Old May 24th, 2000, 12:39 AM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Have you read my example at all? http://www.devshed.com/Talk/Forums/Forum6/HTML/000166.html

>>#1 how do i delete a line...?
Remove the following will DELETE instead of ADJUST:
open(OUT,">>$out");
flock (OUT, 2);
print OUT "$FORM{'num'}|$FORM{'ip'}n";
close(OUT);

>> have two submit buttons... "adjust" and
"delete"...
On your html form, assign field name to each of the two submit buttons..
<input type=submit name=adjust value=Adjust>
<input type=submit name=delete value=Delete>

Look back to my previous example, the 'Parse Form' could be something like..

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<!--(.|n)*-->//g;
$value =~ s/<([^>]|n)*>//g;
$value =~ s/^s+//;
$value =~ s/s+$//;
$FORM{$name} = $value;
}
# next, determine action type
if ($FORM{'delete}) {
&delete;
elsif ($FORM{'adjust'}) {
&adjust;
}
else {
print "Unable to determine action typen";
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > subroutine and deleting a line in a file


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway