damian What you are dealing with is more of a concurrency issue.

I suggest trying to reduce the webpage to one call of this script. However, if you don't you will have to help your script out more than you do now.

Basically you have to use file locking, and then also write your script like an iterator function. That is, where you see the while loops you need remove them and instead store state information in files that will allow you to simulate the while loop over multiple calls of the script.

Here is sample code to give you an idea of what I'm talking about (because it's getting late and I'm low on caffeine :) -- so I know I'm not entirely clear).

simple.pl

#!/usr/bin/perl #This program counts from 1 to 10 my $count = 1; while($count <= 10) { print $count,"\n"; $count++; }

iterator.pl

#!/usr/bin/perl #This program prints out 1 to 10, but only one number #per call to the program if (not -e "/tmp/iterator-state") { `echo 1 > /tmp/iterator-state`; } open(S, "/tmp/iterator-state"); chomp(my $count = <S>); print $count,"\n"; $count++; close(S); if ($count <= 10) { `echo $count > /tmp/iterator-state`; } else { unlink "/tmp/iterator-state"; }

The major problem with this last approach is that it's very very very BAD. That is, you need to do alot of locking that I'm not doing in the example, and whole sorts of other things can go wrong.

Therefore, I suggest you change your requirements. Either do it like my original sample does (that is, run the script only once and get all the results then). Alternatively, you can split the ad file into three separate files, and then run your original code on it (but of course once on each file).

The last method I just mentioned can be jury-rigged to act more like what you want. You can periodically change the three files to be three new files with different subsets. That way you don't have the same 1/3 of the ad's appear on each 3 ad's.

Enjoy!
Gryn


In reply to Call lots of em (uck) by gryng
in thread Reading files sequentially by damian

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.