Solved

writing and reading prob.

Posted on 1998-06-04
6
196 Views
Last Modified: 2010-03-04
This is part of code is from a bbs im designing.  I keep a txt file of all the forums up, it looks like this without the ()'s
(the name of the forum)::(topic number)::(description)::(#of posts)::(last date of post)::(moderator)::(is it on or off?)
Here is the code, I just want it to open the file, update the post number, but its not working when i run it, it prints whatevers in the file again, and doesn't make the update.  Any questions please post.  thanks.  Oh yeah, $Forum is the name of the forum, like someone posts something in the work forum, $Forum will be set to work and $name will also equal this.
      &LockFile("$main_forums");
      open(FORUMS,"+<$main_forums");
      @lines = <FORUMS>;
      foreach $line (@lines) {
            chop ($line) if ($line =~ /\n$/);
            ($name,$topicnum,$descrip,$posts,$date,$mod,$onoff) = split(/::/, $line);
            if ($name eq $Forum) {
                  ++$posts;
                  print FORUMS "$name::$topicnum::$descrip::$posts::$date::$mod::$onoff\n";
            }
            else {
                  print FORUMS "$line\n";
            }
      }      
      close(FORUMS);
      &UnlockFile("$main_forums");
0
Comment
Question by:rkeir
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 1

Expert Comment

by:hank1
ID: 1207382
don't think first write will trunc a file open +<
Are you running into you delimiter ::  seems you never hit
0
 
LVL 4

Accepted Solution

by:
unicorntech earned 50 total points
ID: 1207383
Open the file handle like so:
open (FORUMS, "$main_forums");
then the line that is ++$posts should be:
$posts = $posts++;

I tried this code set out like this and it worked fine

Regards,

Jason
0
 

Author Comment

by:rkeir
ID: 1207384
I don't think its writing to the file i tried exactly what you said and it wasn't working for me, it looked like it just opened it, read, and then closed.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:alamo
ID: 1207385
>then the line that is ++$posts should be:
>$posts = $posts++;

That part is wrong, because it's ambiguous and thus implementation-specific behavior.

In fact on my version of perl it fails, $posts is never incremented, which means the same line is written back out as is read. It sounds like your perl does the same.

Change that line back to the original version, and it might work.

0
 

Author Comment

by:rkeir
ID: 1207386
Thanks for everyone's suggestions.  The reason on my system, i think, it wasn't printing back was it was using :: as something else, when i used | instead as a delimiter, it worked.  Oh well, thanks.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1207387
#is this perl4 or perl5?
#did you run it with -w?
open(FORUMS,"+<$main_forums") or die "can't open $main_forums because $!";
   @lines = <FORUMS>;
seek(FORUMS,0,0); #warning: this assumes the length of the lines do not change
   foreach $line (@lines) {
#   chop ($line) if ($line =~ /\n$/);
    chomp($line); #is an easier way to say this
   ($name,$topicnum,$descrip,$posts,$date,$mod,$onoff) = split(/::/, $line);
   if ($name eq $Forum) {
   ++$posts;
#   print FORUMS "$name::$topicnum::$descrip::$posts::$date::$mod::$onoff\n";
   print FORUMS "$name\::$topicnum\::$descrip\::$posts\::$date\::$mod\::$onoff\n";
# $Package::Variable is how you refer to names in other modules
# I don't think that's what you meant here.
  }

#a simpler way to do it might be
{local $^I=".old";local @ARGV=($main_forums);
while( <> ){
        s/^(\Q$Forum\E::([^:]*::){2})(\d+)/$1${\($3+1)}/;
        print;
}
}
#but without knowing how your &LockFile and &UnlockFile subs work, I can't tell if they would be affected

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question