Hola,
¿Hay alguna función incorporada para convertir una cadena en una lista? Por ejemplo, tengo una cadena "abcd1234cdef5678" y quiero que se convierta a {"abcd", "1234", "cdef, "5678""}.
¡Gracias!
Ahmad
Otra técnica:
parts = {};
rc = Pat Match( "1234567890abcdefghijklmnopqrstuv", Pat Repeat( Pat Len( 4 ) >> parts[N Items( parts ) + 1] ) );
Show( parts );
partes = {"1234", "5678", "90ab", "cdef", "ghij", "klmn", "opqr", "stuv"};
El enfoque de Jim es mejor para cadenas cortas porque la próxima persona que mantenga el JSL lo entenderá. Para cadenas de 10K aproximadamente, la coincidencia de patrones será notablemente más rápida.
The pattern match (red curve) avoids reprocessing the stringThe pattern match (red curve) avoids reprocessing the stringThe pattern match (red curve) avoids reprocessing the stringThe pattern match (red curve) avoids reprocessing the stringThe pattern match (red curve) avoids reprocessing the stringThe pattern match (red curve) avoids reprocessing the stringLa coincidencia de patrón (curva roja) evita reprocesar la cadena
El es el JSL para hacer el gráfico ...
dt = New Table( "Untitled",
Add Rows( 0 ),
New Column( "n", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [] ) ),
New Column( "forloopTime", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [] ) ),
New Column( "patternTime", Numeric, "Continuous", Format( "Best", 12 ), Set Selected, Set Values( [] ) )
);
For( nstr = 1, nstr < 10000, nstr *= 2,
dt << addrows( 1 );
dt:n = nstr * 4; // four substrings in the repeat...
TheString = Repeat( "abcd1234cdef5678", nstr );
start = Tick Seconds();
TheList = {};
i = 1;
While( Substr( TheString, i, 4 ) != "",
Insert Into( TheList, Substr( TheString, i, 4 ) );
i = i + 4;
);
stop = Tick Seconds();
dt:forloopTime = (stop - start);
start = Tick Seconds();
parts = {};
rc = Pat Match( theString, Pat Repeat( Pat Len( 4 ) >> parts[N Items( parts ) + 1] ) );
stop = Tick Seconds();
dt:patternTime = (stop - start);
Wait( 0 );
);
dt << Graph Builder(
Size( 764, 624 ),
Show Control Panel( 0 ),
Legend Position( "Inside Left" ),
Variables( X( :n ), Y( :forloopTime ), Y( :patternTime, Position( 1 ) ) ),
Elements( Points( X, Y( 1 ), Y( 2 ), Legend( 9 ) ), Smoother( X, Y( 1 ), Y( 2 ), Legend( 10 ) ) ),
SendToReport(
Dispatch( {}, "n", ScaleBox, {Min( -1453.48837209302 )} ),
Dispatch( {}, "forloopTime", ScaleBox, {Format( "Best", 12 ), Min( -0.949803149606299 )} ),
Dispatch( {}, "graph title", TextEditBox, {Set Text( "Compare algorithms" )} ),
Dispatch( {}, "X title", TextEditBox, {Set Text( "n patterns " )} ),
Dispatch( {}, "Y title", TextEditBox, {Set Text( "Seconds" )} ),
Dispatch( {}, "400", LegendBox, {Set Title( "" )} )
)
);
La razón por la que el ejemplo del bucle for se ralentiza en cadenas grandes: la función substr debe encontrar la ubicación de cada subcadena, a partir de la comienzo . La coincidencia de patrón se mueve a través de la cadena. una vez .
No conozco tal función, pero podría usar este código o convertirlo en una función
Names Default To Here( 1 );
TheString = "abcd1234cdef5678";
TheList = {};
i = 1;
While( Substr( TheString, i, 4 ) != "",
Insert Into( TheList, Substr( TheString, i, 4 ) );
i = i + 4;
);
</CODE></PRE>
<P>O aquí está como una función.</P>
<PRE><CODE class=" language-jsl">SeparateString = Function( {TheString},
TheList = {};
i = 1;
While( Substr( TheString, i, 4 ) != "",
Insert Into( TheList, Substr( TheString, i, 4 ) );
i = i + 4;
);
TheList;
);
MyList = SeparateString("abcd1234cdef5678");
Hola Jim,
¿Cómo abordaría esto si tiene una cadena y una coma es donde desea dejar de separar la cadena?
Por ejemplo,
convirtiendo esto " C = "Hoy está lloviendo muy, muy fuerte" a b = {"Está", "lloviendo", "muy", "hoy fuerte" }
¡Gracias!
Utilice la función Words() para dividir la cadena en una Lista con los elementos de cada una de las subcadenas identificadas por el delimitador especificado, en su caso una coma.
names default to here(1);
C = "It is, raining, very, hard today";
b = Words( C, "," );
Si convierte una cadena en una lista de caracteres individuales (un poco diferente de la solicitud de OP pero coincide con el título), entonces puede hacer:
Items( "hello", "" );
Que devuelve:
{"h", "e", "l", "l", "o"}