Solved

writing and reading prob.

Posted on 1998-06-04
6
189 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 6

Expert Comment

by:alamo
Comment Utility
>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
Comment Utility
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
Comment Utility
#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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Perl for loop for 2000 ms 7 91
Question about @INC variable in perl 1 50
Awk Question 2 116
batch script for automated email 12 94
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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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