Trihedralguy has asked for the wisdom of the Perl Monks concerning the following question:

Basically this should be pretty simple, but I dont know what im doing:
I have a webpage where a user can add and delete members from a .htaccess file. First I read the file in and the get rid of "required user" as its not needed nor is it a user. Then I split the usernames into spaces (the username will never have a space.) I then look to see which user the user click DELETE next to and delete that user from its array by setting its array to nothing, then I join them together and print them out. I'm getting close, cause it messes up the text file everytime i try and delete someone.
my (@delete, @userid, @username); $delete[$subcounter] = param("Delete"); $userid[$subcounter] = param("userid"); $username[$subcounter] = param("username"); my $othercount = 1; while (@personal[$othercount]) { if ($personal[$othercount] eq $username[$othercount]) { $personal[$othercount] = ''; } else { } $othercount++; } my $str = ''; $str = join('require user', $personal[1], $personal[2], $personal[3], +$personal[4], $personal[5], $personal[6], $personal[7], $personal[8], + $personal[9], $personal[10]); open FILE, ">.htaccess"; print FILE $str; close FILE;
The variables at the top are the results of the form input. Please tell me what im doing wrong.

Replies are listed 'Best First'.
Re: Reading in and writing files
by GrandFather (Saint) on Jun 11, 2007 at 20:49 UTC

    Things you are doing wrong:

    1. Not indenting your code
    2. Not providing a working sample
    3. Not providing sample data
    4. Not providing sample output
    5. Not providing expected output
    6. Using a while loop where most likely you should use a for loop
    7. Apparently using 1 based indexing for arrays (Perl uses 0 normally)
    8. Using an empty else
    9. Explicitly listing all the elements in an array rather than using a slice
    10. (probably wrong) omitting a space in the string for join
    11. (possibly wrong) using a variable ($subcounter) without apparent initialisation
    12. Describing a problem you haven't provided code for

    The last one is the kicker!


    DWIM is Perl's answer to Gödel
Re: Reading in and writing files
by Popcorn Dave (Abbot) on Jun 12, 2007 at 06:12 UTC
    To add to what GrandFather said, have you considered trying this as a non-cgi problem and getting it to work that way first? Then when you see where and why it works, you can easily convert it to cgi. Also, you can use the Perl GUI debugger to watch your program's execution.


    Revolution. Today, 3 O'Clock. Meet behind the monkey bars.

    I would love to change the world, but they won't give me the source code

Re: Reading in and writing files
by Trihedralguy (Pilgrim) on Jun 11, 2007 at 19:57 UTC
    #Declare them outside the loop or else we will just over-write them. my (@delete, @userid, @username); $delete = param("Delete"); $userid = param("userid"); $deletename = param("deletename"); my $othercount = 1; while (@personal[$othercount]) { if ($personal[$othercount] eq "$deletename") { $personal[$othercount] = ''; } else { } $othercount++; } my $str = ''; $str2 = join(' ', $personal[1], $personal[2], $personal[3], $personal[ +4], $personal[5], $personal[6], $personal[7], $personal[8], $personal +[9], $personal[10]); $str = "require user $str2"; open FILE, ">.htaccess"; print FILE $str; close FILE; print " <span id=\'p1\' style=\'\'> <div class=\"finish\"> <div class=\"finish2\"> <img src=\"aimages/$image.jpg\" width=\"84\" height=\"84\" /></div><di +v class=\"finish3\"> <h1 class=\"style1\">$errtitle</h1> <p class=\"style1\">$errmsg<br /></p></span>"; } exit;
    Im getting closer, but now instead of deleting the selected user, it deletes the first user. (Always)
Re: Reading in and writing files
by aquarium (Curate) on Jun 12, 2007 at 03:24 UTC
    if you are using the "standard" .htaccess apache files, then they're not plain text, and are maintained by using htpasswd utility
    the hardest line to type correctly is: stty erase ^H