Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone,

I am creating a script where users can subscribe to multiplt mailing lists, I am storing the emails in seperate text files, say one for each newsletter.

I am also running a contest where one of the people who sign up will win one of three prizes which they select when they sign up, Therefor I somehow need to gather all the emails and pick a random one to be fair. The contest will run for say four weeks then a new one will start. But I will have the option for people to reenter the contest every month so as to prevent people joining mailing lists twice.

Obviously the above creates problems for me, I dont have access to SQl or anything so I cant use a database, only flat files to store the emails.

Is there an easy way to do this, the current way I am having to do this is to get the script to ...
- write to a txt file for that newsletter
- write to a txt file for that contest. Obviously there are three seperate ones.
- write to a main txt file so i can prevent them from resigningup -

Is there an easier way to do this ? This will use huge ammounts of space... Thanks for any help, John

Replies are listed 'Best First'.
(ar0n) Re: Flat Files
by ar0n (Priest) on Mar 05, 2001 at 20:42 UTC
    Check out DBD::CSV. It allows you to have a DBI interface to a text-file. Instead of actually using a database, you simply use a commma-seperated text file.

    Just my $0_02;


    [ar0n]

Re: Flat Files
by davorg (Chancellor) on Mar 05, 2001 at 20:46 UTC

    DBD::CSV is a good idea, but last time I looked at it there was very quite a long list of other modules that it relied on.

    Have you considered tieing a hash to a DBM file? That would give you random access to the records and the DBM module will take care of all the reads and writes.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: Flat Files
by azatoth (Curate) on Mar 05, 2001 at 20:51 UTC
    Assuming you would like to read the email addies into an array via a filehandle, you could always use rand. Like this : -

    srand; # initialize random UPDATE : This is only needed for earlier +versions of Perl, thanks to Albannach for telling me :) $random_email_address = $email [rand @email]; #assuming your array of +email addresses is named @email print $random_email_address;
    This will do the random bit for you, in a very simple, low-level way. I'm sure someone will post some hardcore code that randomises via regex and sorts out your size issues with some DB stuff etc...

    Just my two cents...

    Azatoth a.k.a Captain Whiplash

    Get YOUR PerlMonks Stagename here!
    Want to speak like a Londoner?
Re: Flat Files
by skazat (Chaplain) on Mar 06, 2001 at 00:17 UTC

    you may want to first think of how you're going ot handle the mailing list itself and then figure out the details. You may want to take an already existing script and tweak/build on top of it.

    check out:

    Mail Man - http://list.org

    Majordomo - http://greatcircle.com/majordomo

    Subscribe Me - http://cgiscriptcenter.com

    <shamless plug>
    Mojo Mail http://mojo.skazat.com
    </shamless plug>

     

    -justin simoni
    !skazat!

Re: Flat Files
by Anonymous Monk on Mar 05, 2001 at 20:57 UTC
    Thanks all, about srand..The file that contains the emails will get huge, I am using one main text file to store all emails signed up..we may be talking about 1-2k per day emails. Can it handle this ?
      Depends on the OS you're using.
      Filesize is not a Perl issue, as far as I know.
      However, on a pre 2.4 linux Kernel, the file limit is 2GB.
      BSD variants don't have this and can go significantly above this limit (far more than you'll get in a few weeks of plain text).
      The same goes for NT and Novell. Not sure about the win 9x variants tho.

      HTH,

      Malk
        Actually file size can be a Perl issue. If you have Perl and compile without large file support, then you will be limited to 2 GB files, even if your OS can handle much larger. (Not that this matters to most people.)
      Please explain the reasoning for doing this -- it seems suboptimal.

      update

      Fine. that may be a bit terse.
      The reason why I ask is because you seem to be ignoring some decent advice that will change your methodologies, and requesting help wastefully shoring up something that may not scale to the level you expect it will.