Solved

writing and reading prob.

Posted on 1998-06-04
6
195 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl efficient DB Call 8 96
problem with using the glob function 1 77
Perl string filter 5 84
Get a Perl script to return content from a module 7 83
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

763 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