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

Hello: This script is part of the admin function to delete a user from the database. I'm using a regular file for the database because the largets it'll get is probably 12 members. I've been working with this code for several days and yet still having problems getting it to work! :( The code puts the existing content of the user.dat file into a array, then I split the content into key/pairs of a hash. Then I rewrite the file and print the hash out. Any ideas why its not working?

open (FILE, "$datafile") or die "Can't read: $!"; while (<FILE>) { ($user, $pw) = split "\|"; $field{$user} = $pw; foreach (keys %field) { print <<EOF; $field{$_}<BR> EOF } } @users = split(/,/,$INPUT{'users'}); #i'm using checkboxes #foreach $users (@users) { delete $field{'$users'}; print <<EOF; $users #debug EOF #}#foreach users open (FILE, ">$datafile") or die "Can't write: $!"; foreach (keys %field) { print FILE "$_\|$field{$_}\n"; print <<EOF; $field{$_}<BR> EOF } close(FILE); }

Replies are listed 'Best First'.
Re: File editing problem
by gumby (Scribe) on Jun 16, 2002 at 19:00 UTC
    You should be using a tie'd hash.
    tie %users, 'Tie::File', $datafile or die $!;
      Just in case your wondering what tie is, check out this

      ~~rob
      ____________________________________________________________
      eval pack "h*", "072796e647022245d445f475454494c5e622b3";

Re: File editing problem
by Aristotle (Chancellor) on Jun 16, 2002 at 20:14 UTC
    use CGI or die;

    Really. The $INPUT{'users'} indicates you're using something else, which is a might bad idea for many reasons.
    my @delete_users = param('users'); my %user = map { /^([^\|]*)\|(.*)/ }, <FILE>; close FILE; delete %user{@delete_users}; open (FILE, ">$datafile") or die "Can't write: $!"; while(my ($usr,$pwd) = each %user) { print FILE "$usr|$pwd\n"; print "$pwd<br>"; } close(FILE);
    ____________
    Makeshifts last the longest.
Re: File editing problem
by dda (Friar) on Jun 16, 2002 at 19:05 UTC
    Don't you confuse @users and $users? And what is a purpose of typing $field{'$users'};? Single quotes prevent $users variable to be interpolated...

    --dda

      Yes they do. The code is deleting the hash value stored under the key called (literally) $users, rather than the hash value stored under the key found in the variable $users. That's one reason the code won't work..

      Makeshifts last the longest.