You can split a string on a character to make it an array with the split
function, but that returns an array, not a list that can be split again.
Or can you do something like:
var myArray = str.split("*");
var myArray.split("|");
I wasn't sure that sort of thing was possible unless perhaps you can
turn the resultant array back into a string. That's the beauty of
ColdFusion turning strings (read: lists) into array's on the fly. List
functions return other lists, not arrays, but they're handled as arrays
internally. If at anytime I want to turn a list into an array object
myself, I can use ListToArray(myList)... or ArrayToList(myArray) conversely.
My specific example was supposed to show how one string can be treated
as three (or more really) different arrays, without actually turning
them into array objects. The string itself never really changes it's
always the same, but CF allows me to manipulate it as an array.
Using split, I end up either changing the original string, or creating a
brand new variable that is an array, and only an array. It can't then be
treated as a string without perhaps running some function on it, or
maybe iterating over the array and creating a new string out of its
elements.
Lists rock. :o)
Cheers,
Chris
Yehuda Katz wrote:
Easily with Ruby. "a,b,c,d|d,e,f,g*h,i,j,k|l,m,n,o".split("*") returns
an array of the two strings, which can then be further processed. I
believe that PHP has similar functionality.
-- Yehuda
On 11/22/06, *Christopher Jordan* <[EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]>> wrote:
I agree that it's a matter of opinion. But I've gotta say that I *LOVE*
being able to treat *any* arbitrary string of text as a list (read:
array). Let me give you an example:
<cfset TheList = ListRest(
CGI.HTTP_REFERRER,"?")>
<cfloop index="i" from="1" to="#ListLen(TheList,"&")#">
<cfset ThisItem = ListGetAt(TheList,i,"&")>
<cfif ListFirst(ThisItem,"=") EQ "numPageID">
<cfset MyNumPageID = ListLast(ThisItem,"=")>
</cfif>
</cfloop>
This code allows me to treat one string of characters (the http_referrer
in this case) as three different arrays. I didn't have to create an
array out of any of it. The very powerful "List" functions allow me to
accomplish this, and CF handles the conversion to arrays eternally. This
is *not* to say that I can't create and manipulate arrays directly, and
that I *have to* use lists instead of arrays, that's just not true. I
can use the ArrayNew() or StructNew() functions to create arrays and
structures that I can easily reference and manipulate.
Er, you are aware that you can reference strings as arrays in PHP, or if
needed convert a string to a real array in one line, right?
for ($i=0; $i < strlen($string); ++$i) {
print $string[$i] . "\n";
}
foreach (explode($string) as $char) {
print $char . "\n";
}
How do you tell PHP what the list delimiter is? In ColdFusion I
can take a list like this:
<cfset str = "a,b,c,d|d,e,f,g*h,i,j,k|l,m,n,o">
I can then turn around and get the first element based on the
asterisk being the delimiter and then treat the result
(a,b,c,d|d,e,f,g) as a new list who's delimiter is the pipe and
then end up with last list whose delimiter is the comma.
<cfloop index="i" from ="1" to="#ListLen(str, '*')#">
<cfset str2 = ListGetAt(str, i, "*")>
<cfloop index="n" from="1" to="#ListLen(str2, '|')#">
<cfset str3 = ListGetAt(str2, n, "*")>
<cfloop index="j" from="1" to="#ListLen(str3)#">
<!--- do some stuff with the innermost elements of
the list --->
</cfloop>
</cfloop>
</cfloop>
Can you do that in php?
Cheers,
Chris
_______________________________________________
jQuery mailing list
discuss@jquery.com <mailto:discuss@jquery.com>
http://jquery.com/discuss/
--
Yehuda Katz
Web Developer | Wycats Designs
(ph) 718.877.1325
------------------------------------------------------------------------
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/
------------------------------------------------------------------------
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.430 / Virus Database: 268.14.11/543 - Release Date: 11/20/2006
9:20 PM
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/