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.

In reply to Re: How do i delete my guestbook's data ? by Revelation
in thread How do i delete my guestbook's data ? by 12monkey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.