Return to the guestbook discussion

Subject: Re: The guestbook
   Date: Wed, 06 Oct 1999 12:56:39 -0700
   From: John Callender 
     To: [name deleted]

[name deleted] wrote:
> 
> Hi
> 
> First of all i want to tell you what a great tutorial you have put
together!
> 
> My question: I'm using your guestbook script.. how do I make the last
entry
> appear on the top instead of the bottom?
> 
> Thank you.

Turn your monitor upside-down?

Sorry; couldn't help myself.

Thank you very much for the kind comment. It means a lot to me to hear
that people are getting something out of the tutorial.

I haven't actually tested this, but I think the way I'd try to do what
you're describing is to modify the part where the new entry is added to
the existing ones, so it is added at the beginning instead of at the
end:

Instead of this:

    # open non-destructively, read old entries, write out new

      sysopen(ENTRIES, "$data_file", O_RDWR)
                               or die "can't open $data_file: $!";
      flock(ENTRIES, 2)        or die "can't LOCK_EX $data_file: $!";
      while(<ENTRIES>) {
          $all_entries .= $_;
      }
      $all_entries .= $entry;

I would do this:

    # open non-destructively, read old entries, write out new

      $all_entries = $entry;
      sysopen(ENTRIES, "$data_file", O_RDWR)
                               or die "can't open $data_file: $!";
      flock(ENTRIES, 2)        or die "can't LOCK_EX $data_file: $!";
      while(<ENTRIES>) {
          $all_entries .= $_;
      }

Hmm. Then, later, when it gets split up into an array and the oldest
ones removed, you'll have to change things so you remove from the *end*
of the list, rather than the beginning of the list, so instead of this:

    if ($max_entries) {

          # lop the head off the guestbook, if necessary

          @all_entries = split(/<HR>/i, $all_entries);
          $entry_count = @all_entries - 1;

          while ($entry_count > $max_entries) {
              shift @all_entries;
              $entry_count = @all_entries - 1;
          }

          $all_entries = join('<HR>', @all_entries);

      }

You would probably want something like this:

    if ($max_entries) {

          # lop the tail off the guestbook, if necessary

          @all_entries = split(/<HR>/i, $all_entries);
          $entry_count = @all_entries - 1;

          while ($entry_count > $max_entries) {
              pop @all_entries;
              $entry_count = @all_entries - 1;
          }

          $all_entries = join('<HR>', @all_entries);

      }

The difference there is that I've used the 'pop' function, which pops an
item off the end (or bottom, or righthand side, depending on how you
look at things) of an array, instead of 'shift', which shifts an item
off the beginning (top, lefthand side) of an array.

Thanks again for the nice message.

John

Return to the guestbook discussion