Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

writing and reading prob.

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
rkeir
Asked:
rkeir
1 Solution
 
hank1Commented:
don't think first write will trunc a file open +<
Are you running into you delimiter ::  seems you never hit
0
 
unicorntechCommented:
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
 
rkeirAuthor Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
alamoCommented:
>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
 
rkeirAuthor Commented:
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
 
ozoCommented:
#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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now