#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 1999
    Posts
    18
    Rep 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;

    }
  2. #2
  3. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    Have you read my example at all? http://www.devshed.com/Talk/Forums/F...ML/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";
    }

IMN logo majestic logo threadwatch logo seochat tools logo