Somewhat new to perl have a loop and random gen question - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Somewhat new to perl have a loop and random gen question

rooster409rooster409 Posts: 1Member
Hi, I've done some minor stuff in the past with perl, form processing and flat file database work, and a little MySQL programming.. Heres my question:

I am working on a random ad utility. I have 3 spaces on my web page for the ads to be placed, and I want each one of them to be different. So far, I have this working using the following code:

# Get 3 random numbers from the range to use as img locations
@chosen = grep {$::z++ < 3} sort { int(rand() * 2) } (1..$num), $::z = 0;
($img1, $img2, $img3) = @chosen;

And that works for getting my 3 randomly chosen numbers. (by the way, $num is the total numbers of "Rows" I have in a specific table inside a database, it will change as advertisers ad their stuff).

The problem I can't figure out is how to do the following:

1. Determine what ads have run out of showings (each ad starts with 3000 views, decreases by 1 each time the ad is shown, I don't have the code for this working yet either). This information is kept in a "Remains" column of the database.

2. IF the number pulled by the random generator above has a "remains" of "0" then it needs to generate another number. Problem with this, is It could possibly choose the same number again, or another number that has "0" views left, and it would display the ad regardless of the "0".

3. I want to figure out a way to pull the row "ID" number into an array, and then remove all of them that have "0" in the remains column. ("ID" is the name of the first column in the table, this is an autoincremented number from 0 on up).

If you have any ideas or could shed some light on what I am doing, please let me know.

Thanks in advance.

Comments

  • JonathanJonathan Posts: 2,914Member
    : Hi, I've done some minor stuff in the past with perl, form
    : processing and flat file database work, and a little MySQL
    : programming.. Heres my question:
    :
    : I am working on a random ad utility. I have 3 spaces on my web page
    : for the ads to be placed, and I want each one of them to be
    : different. So far, I have this working using the following code:
    :
    : # Get 3 random numbers from the range to use as img locations
    : @chosen = grep {$::z++ < 3} sort { int(rand() * 2) } (1..$num), $::z = 0;
    : ($img1, $img2, $img3) = @chosen;
    Hmmm....that grep/sort etc line looks kinda familiar. ;-) You can do what you've done in one line:-

    ($img1, $img2, $img3) = grep {$::z++ < 3} sort { int(rand() * 2) } (1..$num), $::z = 0;

    : And that works for getting my 3 randomly chosen numbers. (by the
    : way, $num is the total numbers of "Rows" I have in a specific table
    : inside a database, it will change as advertisers ad their stuff).
    Uh....and what if they remove something? Be careful. In general, row number is not equal to the primary key. So you want to do the following query:-

    SELECT id
    FROM ads

    Using your field and table names. Then get all the ids and put them in an array, e.g. called @allads. Then just do:-

    ($img1, $img2, $img3) = grep {$::z++ < 3} sort { int(rand() * 2) } @allads, $::z = 0;

    : The problem I can't figure out is how to do the following:
    :
    : 1. Determine what ads have run out of showings (each ad starts with
    : 3000 views, decreases by 1 each time the ad is shown, I don't have
    : the code for this working yet either). This information is kept in
    : a "Remains" column of the database.
    Easiest way is to decrement the Remains column each time you show the ad. Then (thinking about the above) I suggest you get a list of possible rows, but identified by the primary key. You can put something in the where clause to eliminate those where Remains = 0. E.G.

    SELECT id
    FROM ads
    WHERE Remains > 0

    : 2. IF the number pulled by the random generator above has
    : a "remains" of "0" then it needs to generate another number.
    : Problem with this, is It could possibly choose the same number
    : again, or another number that has "0" views left, and it would
    : display the ad regardless of the "0".
    If you use what I just suggested, those with a "remains" of "0" never make it into the list of choices, so you won't have this problem.

    : 3. I want to figure out a way to pull the row "ID" number into an
    : array, and then remove all of them that have "0" in the remains
    : column. ("ID" is the name of the first column in the table, this is
    : an autoincremented number from 0 on up).
    Based on this, all I've told you is pretty much spot on. But you can do the removal in the SQL query to save you the work of doing it in your own code.

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.