Solved

writing and reading prob.

Posted on 1998-06-04
6
192 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl to convert excel to csv 3 182
Executing multiple sybase statements in perl dbi 2 88
Perl Awk Need Help 3 109
Perl modules on linux ec2 3 96
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now