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

i made a guestbook and i want to add some function. eg:del post and modify post i can do the del function , but don't know how to do the modify, below is my admin.pl
#!/usr/bin/perl print "content-type:text/html\n\n"; $admin ="sonic"; if($ENV{'REQUEST_METHOD'} eq "POST"){ read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'}); }else{ $buffer=$ENV{'QUERY_STRING'}; } @pairs=split(/&/,$buffer); foreach $pair(@pairs){ ($name,$value)=split(/=/,$pair); $value=~tr/+/ /; $value=~s/%([0-9a-fA-F][0-9a-fA-F])/pack("C",hex($1))/eg; $value=~s/</&lt;/g; $value=~s/\n/<br>/g; $FORM{$name}=$value; } open(READ,"guestbook.dat"); @lines=<READ>; close(READ); my $pw = $FORM{'password'}; my $adminmsg = $FORM{'ownermsg'}; my $modnum = $FORM{'postid'}; if ( $admin eq $pw ) { foreach $line(@lines) { ( $number,$sign_name,$sign_email,$sign_homepage,$sign_icon,$sign_time, +$sign_msg ) = split ( /__/, $line ) ; if ( $modnum ne $number ) { push ( @data , $line ); open(WRITE,">guestbook.dat"); print WRITE @data; close(WRITE); } } print "Loading ..."; print "<META HTTP-EQUIV=Refresh CONTENT=2;URL=viewguestbook.pl?action= +view_post&start=0&end=$show>"; }else{ print <<EOF; <center>wrong password<br> Please <a href=javascript:window.history.back()>go back</a> ! </center> EOF }
if i want modify a post message($sign_message)only,
then write back into guestbook.dat ,
how to write back to the orginal place ,
is it use sort and resever ?
hope someone can understand my question!!

Replies are listed 'Best First'.
(dooberwah) Re: how can i modify the array ?
by dooberwah (Pilgrim) on Jun 05, 2002 at 13:37 UTC
    Greetings

    First of all, You really should use CGI.pm, strict, and warnings. With that said: you can easily modify $sign_msg and then join all the variables back one string.

    ($number, $sign_name, $sign_email, $sign_homepage, $sign_icon, $sign_t +ime, $sign_msg ) = split ( /__/, $line ) ; $sign_msg = "Hello, World!\n"; # Do your modification here $newline = join ( /__/, ($number, $sign_name, $sign_email, $sign_homep +age, $sign_icon, $sign_time, $sign_msg ); push @newarray, $newline;

    -Ben Jacobs (dooberwah)
    http://dooberwah.perlmonk.org
    "one thing i can tell you is you got to be free"

      thx for reply
      use join can modify the sign_msg
      but how to write it back in the middle of the array?

      4__taka__email__url__icon__time__message
      3__12monkey__email__url__icon__time__message
      2__sarah__email__url__icon__time__message
      1__john__email__url__icon__time__message
      (after i modify third message , how to write into the orginal place ?)
      if ( $admin eq $pw ) { foreach $line(@lines) { ( $num,$sign_name,$sign_email,$sign_homepage,$sign_icon,$sign_time,$si +gn_msg ) = split ( /__/, $line ) ; if ( $modnum < $num ) { push @largedata , $line ; } elsif ( $modnum eq $num ) { $sign_msg="$adminmsg"; $new = join ('__',( $modnum , $sign_name , $sign_email , $sign_hom +epage , $sign_icon , $sign_time , $sign_msg)); push @modifydata , $new; } elsif ( $modnum > $num) { push @lessdata , $line ; } } open(WRITE,">guestbook.dat"); print WRITE @largedata; print WRITE @modifydata; print WRITE "\n"; print WRITE @lessdata; close(WRITE);
      yeah , i have done it
      anything need to improve?