Next | Lightweight Databases | 58 |
Here $file refers to a tied array instead of a filehandle:
sub delete_user { my ($file, $target_user) = @_; for my $n (0 .. $#$file) { my ($user) = split /:/, $file->[$n]; next unless $user eq $target_user; splice @$file, $n, 1; last; } }
Or we might even use this:
sub delete_user { my ($file, $target_user) = @_; @$file = grep !/^$target_user:/, @$file; }
Wasn't that easy?
Downside: The short version reads the entire file into memory
Next | Copyright © 2003 M. J. Dominus |