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

i made a guestbook , the data saved in a text file
====data==== 3__matt__msg 2__sarah__msg 1__12monkey__msg
if i want to delete sarah's post , how can i do ? can give me a hand ?

Code tags - dvergin 2002-03-29

Replies are listed 'Best First'.
Re: How do i delete my guestbook's data ?
by Revelation (Deacon) on Mar 29, 2002 at 05:24 UTC
    Flat files generally have one major problem, and that is their innability to delete information. In order to delete information you will have to put all that data into an array or string, and extract the data that you wish to delete.

    One way to make data easyer to work with is to realize that you can generally remove data the same way you read it, or by ommiting the portion you wish to delete in your read. After you have ommited the portion you wished to read, or written everything except that portion into a new string or array, you can rewrite the database using the write over everything flag ">".

    You are probably reading your data using split of __, but I'm not exactly sure how you are seperating input. (newlines, maybe?).

    If each peace of data is seperated by a newline you can use a for loop like so:

    Update: Code has been commented for user 12monkey, to explain each step.
    my $data = "/path/to/file.txt"; # Path to the datafile. open(FILE,"+<$data") || die("Can't open database - $!\n"); #Reads the + file, you can use <$data, in fact, and <$data should be used. chomp(@lines = <FILE>); #Gets rid of newlines close(FILE); # Closes the file my @line; foreach my $line (@lines) { #Foreach line in the file. my ($usernumber,$username,$guestbookinfo) = split(/\__/, $line); # We + will split it up using __ as our delimiter. # The new values of each field (in order) will be $usernumber,$userna +me,$guestbookinfo. $line[$usernumber][0] = $username; # perldoc PerlDSC. This is a mul +tidimensional array. It is used used in order to organize our array, + using the usernumber, and 0 for user name... $line[$usernumber][1] = $guestbookinfo; # Or 1 for the information in the guestbook. } # If we were to call $line[0][1], we would be given the value of the g +uestbook information of the user who lies in the first line of the da +ta file! open(FILE,">$data") || show_die("Can't open database - $!\n"); # Thi +s was wrong last time. It should be >, as we're going to completely +rewrite the file. for my $family (reverse(1 .. $#line) ) { #the @line array has a certa +in amount of values in it (0 to some number), $#line will give us the + amount of values. # By reversing 1 .. the amount of values, we can print everything int +o the data file, just like you wanted it to be printed (with the high +est first.) unless ($line[$family][0] eq "$user_to_delete") {print FILE $family."_ +_".$line[$family][0]."__".$line[$family][1]."\n";} # Unless the username ($line[$family][0]) (refer to the foreach code) + is the username we wish to delete, which you will have to assign usi +ng CGI.pm or some form parser before this starts, we print out all th +e information. } #The for statement will go backwards through each line, until we hit t +he first line. Since you started usernumber at 1, we end the for loo +p at one. close(FILE); #Close the file, and we're done!
    Update
    Anything you didn't understand?

    Just use however you are seperating your input lines, and substitute it for the foreach $line (@lines). You are probably using a foreach loop anyways.

    The code uses an array of an array to parse throrugh each user, and print them into the file backwords, just like you have done. The code is not strict compliant, nor properly debugged, but it works fine on a database using newline seperators, with __ to seperate fields.
      yes , i used new line to save the data and __ to split it, i have read your code many times , but still don't know how does it work , may be my english is poor ! can you explain each step ? open(FILE,"+<$data") #what does mean "(+<$data") ? i just see (FILE,">filepath")(FILE,">>filepath") and (FILE,"filepath"), ------------- $line$usernumber[0] = $username; $line$usernumber1 = $guestbookinfo; can give some example ?
      yes , i used new line to save the data and __ to split it, i have read your code many times ,
      but still don't know how does it work ,
      may be my english is poor !
      can you explain each step ?
      open(FILE,"+<$data") #what does mean "(+<$data") ?
      i just see (FILE,">filepath")(FILE,">>filepath") and (FILE,"filepath"),
      -------------
      $line[$usernumber][0] = $username; $line[$usernumber][1] = $guestbookinfo;
      can give some example ?
      very clear , thx a lot!!
Re: How do i delete my guestbook's data ?
by premchai21 (Curate) on Mar 29, 2002 at 00:15 UTC

    Depends. You're being very vague. The general idea is, figure out the format of the text file, then delete the portion that corresponds to the post in question, assuming a 1:1 correspondence. Read the code of the program to which you refer. If you want anything more you'll have to be more specific I'm afraid.