SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: array(question)

  1. #1
    SitePoint Member
    Join Date
    Jan 2003
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation array(question)

    Hey all

    I have a small array question for ya!

    I have an array of five things. I generate a random number and use it to pull out one of the five things in the list.

    BUT! What if I wanted to use that random number to create a STARTING POINT in an array. In other words to,say, print a list of the array with the first element in the list decided by the random number.

    example:

    arr[0]
    arr[1]
    arr[2]
    arr[3]
    arr[4]

    random number is 3, so I want to then print

    arr[2]
    arr[3]
    arr[4]
    arr[0]
    arr[1]

    Does anybody have any idea how I can do this?

    Cheers

    azz
    code... debug... debug... *SIGH* DUBUG!

  2. #2
    S1tepoint Surfer drochili's Avatar
    Join Date
    Aug 2002
    Location
    Mississauga, Canada
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: array(question)

    can't you just create a processing script ie something like this

    function printInOrder(theArray)
    {
    int a = rand(5); --> I dunno the syntax for using rand.. but you get the idea, put the rand number in a variable
    int b = 0; //Counter
    int c = (put array size here); //Num of items in array
    while(b < c)
    {
    //pseudocode below, have to figure out syntax
    if b+a >= arraySize
    print theArray[b+a - arraySize]
    else
    print theArray[b+a]
    counter++
    }
    }

    sorry it's a bit messy. dunno if it answers your question or not ?

    dr
    Originally posted by azzkika
    Hey all

    I have a small array question for ya!

    I have an array of five things. I generate a random number and use it to pull out one of the five things in the list.

    BUT! What if I wanted to use that random number to create a STARTING POINT in an array. In other words to,say, print a list of the array with the first element in the list decided by the random number.

    example:

    arr[0]
    arr[1]
    arr[2]
    arr[3]
    arr[4]

    random number is 3, so I want to then print

    arr[2]
    arr[3]
    arr[4]
    arr[0]
    arr[1]

    Does anybody have any idea how I can do this?

    Cheers

    azz

  3. #3
    SitePoint Member
    Join Date
    Jan 2003
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DUDE! You're the man!

    Yep, that works nicely. If you want, here's the full code I used. It gets quite scary but the logic is PERFECT. Thanks mate!

    <body bgcolor="#FFFFFF" text="#000000" link="#800080" vlink="#800080" alink="#FF0000" leftmargin=0 topmargin=0 marginwidth="0" marginheight="0">
    <% function showPic(){ Response.Write(this.pic); }
    function showText(){ Response.Write(this.text); }
    function showLink(){ Response.Write(this.link); }
    function showTitle(){ Response.Write(this.title); }

    function AdObject(pic, text, link, title)
    { this.pic = pic;
    this.text = text;
    this.link = link;
    this.title = title;

    this.showPic = showPic;
    this.showText = showText;
    this.showLink = showLink;
    this.showTitle = showTitle; }

    var Ads = new Array();
    Ads[0] = new AdObject("This pic","Goto this place","http://www.this.com","Title of this place");
    Ads[1] = new AdObject("Another pic","Goto this place","http://www.else.com","Title of this other place");
    Ads[2] = new AdObject("Third pic","Goto that place","http://www.here.com","Title of my place");

    var p = (Ads.length)
    var AdRandom = Math.round(Math.random()*(p-1));
    var counter = 0;
    var ArraySize = 3; %>

    <table width="100%" border="1" cellspacing="15" cellpadding="0" class="content">

    <% while (counter < ArraySize)
    { if ((counter+AdRandom) >= ArraySize)
    { thePosition = (counter+AdRandom) - ArraySize %>
    <tr>
    <td width="25%"><% Response.Write(Ads[thePosition].showPic())%></td>
    <td width="25%"><% Response.Write(Ads[thePosition].showTitle())%></td>
    <td width="25%"><% Response.Write(Ads[thePosition].showText()) %></td>
    <td width="25%"><a href="<% Response.Write(Ads[thePosition].showLink()) %>">link</a></td>
    </tr>
    <% }
    else {
    thePosition = counter+AdRandom %>
    <tr>
    <td width="25%"><% Response.Write(Ads[thePosition].showPic())%></td>
    <td width="25%"><% Response.Write(Ads[thePosition].showTitle())%></td>
    <td width="25%"><% Response.Write(Ads[thePosition].showText()) %></td>
    <td width="25%"><a href="<% Response.Write(Ads[thePosition].showLink()) %>">link</a></td>
    </tr>
    <% }
    counter++ } %>

    </table>

    kinda funky, huh!

    Thanks again

    azz
    code... debug... debug... *SIGH* DUBUG!

  4. #4
    S1tepoint Surfer drochili's Avatar
    Join Date
    Aug 2002
    Location
    Mississauga, Canada
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Glad to be of help

    dr

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wrote a method for re-ordering arrays like that...just FYI
    Code:
    Array.prototype.reOrder = function( i )
    {
    	return this.concat( this.splice( 0, i ) );
    }
    Should work with most versions of Netscape and IE 5.5+
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •