473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do I change this function from Random to Sequential

290 100+
Hi,

I am using this function which is
used to spin text.

Currently it looks to me like it is selected an
element from the $tStringToken array at random.

These two lines seem to be doing that:
Expand|Select|Wrap|Line Numbers
  1. $i = rand(0,$tStringCount); 
  2. $replace = $tStringToken[$i];  
I would like to change this so that it goes sequentially through
all of the available elements in the array and then when finished
starts again with the first element of the array.

But I am having trouble working it out.

This is the function I am working on.

Expand|Select|Wrap|Line Numbers
  1. function spin($pass){
  2.     $mytext = $pass;
  3.     while(inStr("}",$mytext)){
  4.         $rbracket = strpos($mytext,"}",0);
  5.         $tString = substr($mytext,0,$rbracket);
  6.         $tStringToken = explode("{",$tString);
  7.         $tStringCount = count($tStringToken) - 1;
  8.         $tString = $tStringToken[$tStringCount];
  9.         $tStringToken = explode("|",$tString);
  10.         $tStringCount = count($tStringToken) - 1;
  11.         $i = rand(0,$tStringCount);
  12.         $replace = $tStringToken[$i];
  13.         $tString = "{".$tString."}";
  14.         $mytext = str_replaceFirst($tString,$replace,$mytext);
  15.     }
  16.     return $mytext;
  17. }  
  18.  
Any ideas on how I can do this would be very helpful

Thanks.


.
Oct 12 '13 #1
6 1544
Dormilich
8,658 Expert Mod 8TB
I would like to change this so that it goes sequentially through all of the available elements in the array and then when finished starts again with the first element of the array.
sounds like a job for the InfiniteIterator.
Oct 14 '13 #2
jeddiki
290 100+
Thanks Dormilich,

However, as my code is written in procedural code,
I was hoping to just change a few lines in my above code to
do what I need :)

Is it not so easy then ? ...



.
Oct 17 '13 #3
Dormilich
8,658 Expert Mod 8TB
even in procedural code you can use classes.

and no, a major change such as from random to sequential usually does not go with changing a few lines of code.
Oct 17 '13 #4
jeddiki
290 100+
OK -

Well, I'm gonna sit down and write out what each
of these lines of code means long hand,
then ...

I'll figure out a way to make it go through the array sequentially instead of randomly.

Can't be that hard ... can it ?


I was hoping that someone with a faster brain than mine, or more experience/expertise could quickly see how this could be done.


There are 2 other functions which are used:

Expand|Select|Wrap|Line Numbers
  1. function inStr($needle, $haystack){
  2.     return @strpos($haystack, $needle) !== false;
  3. }
  4.  
  5. function str_replaceFirst($s,$r,$str){
  6.     $l = strlen($str);
  7.     $a = strpos($str,$s);
  8.     $b = $a + strlen($s);
  9.     $temp = substr($str,0,$a) . $r . substr($str,$b,($l-$b));
  10.     return $temp;
  11. }


.
Oct 17 '13 #5
Dormilich
8,658 Expert Mod 8TB
I was hoping that someone with a faster brain than mine, or more experience/expertise could quickly see how this could be done.
I find the iterator version very elegant. once it is set up, all you need to do is call ->next() without having to worry where you are in the array.
Oct 17 '13 #6
Dormilich
8,658 Expert Mod 8TB
Currently it looks to me like it is selected an element from the $tStringToken array at random.
though I doubt $tStringToken is an array ...
Oct 17 '13 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Wayne Cressman | last post by:
I'm writing a function to dynamically change a form validation script depending upon the user's choices. The form onsubmit is: onsubmit="writevalidate(this.select.value);return...
39
by: Alan Isaac | last post by:
This may seem very strange, but it is true. If I delete a .pyc file, my program executes with a different state! In a single directory I have module1 and module2. module1 imports random and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.