Languages :: PHP :: Knowledge Base : How to get run sequences from a string like "AAABCCCCDDAAAAEEE" |
|||
| By: VGR |
Date: 25/03/2005 10:06:05 |
Points: 0 | Status: Answered Quality : Good |
|
Dated 20/03/2003 <?php // //VGR20032003 EE // // initialisations $string="AAABCCCCDDAAAAEEE"; $elements=array(); // array[*] of String in high-level languages // loop $i=0; // counter of chars $j=0; // counter of elements $oldchar=$string[1]; while ($i<strlen($string)) { $curchar=$string[$i]; if ($curchar<>$oldchar) { // add found string before continuing $elements[$j].=$oldchar; $oldchar=$curchar; $j++; } else $elements[$j].=$curchar; // if we broke sequence $i++; } // while string not parsed completely $j++; // end of loop // display for ($i=0; $i<$j;$i++) echo "element $i = '{$elements[$i]}' "; ?> |
|||
| By: VGR | Date: 25/03/2005 10:07:45 | Type : Comment |
|
| Results (add @ lines 15 and 18 to suppress NOTICEs if you run the E_ALL error reporting level) element 0 = 'AAAA' element 1 = 'B' element 2 = 'CCCC' element 3 = 'DD' element 4 = 'AAAA' element 5 = 'EE' |
|||
| By: VGR | Date: 25/03/2005 10:09:07 | Type : Comment |
|
| can be optimized (not computing strlen() in the for() loop declaration, etc) but that's a PHP-specific issue. "Normal" compiled languages do evaluate such a constant once at the start of the loop. | |||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








