Bonjour,
Existe-t-il une fonction intégrée pour convertir une chaîne en liste ? Par exemple, j'ai une chaîne "abcd1234cdef5678" et je veux qu'elle soit convertie en {"abcd", "1234", "cdef, "5678""}.
Merci!
Ahmed
Une autre technique :
parts = {};
rc = Pat Match( "1234567890abcdefghijklmnopqrstuv", Pat Repeat( Pat Len( 4 ) >> parts[N Items( parts ) + 1] ) );
Show( parts );
parts = {"1234", "5678", "90ab", "cdef", "ghij", "klmn", "opqr", "stuv"} ;
L'approche de Jim est meilleure pour les chaînes courtes car la prochaine personne qui maintiendra la JSL la comprendra. Pour les chaînes de 10 Ko environ, la correspondance de modèle sera sensiblement plus rapide.
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 stringLe pattern match (courbe rouge) évite de retraiter la chaîne
C'est le JSL pour faire le graphe...
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 raison pour laquelle l'exemple de boucle for ralentit sur les grandes chaînes : la fonction substr doit trouver l'emplacement de chaque sous-chaîne, à partir de la début . La correspondance de modèle se déplace dans la chaîne une fois .
Je ne connais pas une telle fonction, mais vous pouvez utiliser ce code ou en faire une fonction
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>Ou ici c'est en fonction</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");
Salut Jim,
Comment aborderiez-vous cela si vous avez une chaîne et qu’une virgule est l’endroit où vous souhaitez arrêter de séparer la chaîne.
Par exemple,
convertir ceci " C = "Il pleut très, fort aujourd'hui" en b = {"Il fait", "il pleut", "très", "fort aujourd'hui" }
Merci!
Utilisez la fonction Words() pour diviser la chaîne en une liste dont les éléments sont chacune des sous-chaînes identifiées par le délimiteur spécifié, dans votre cas une virgule.
names default to here(1);
C = "It is, raining, very, hard today";
b = Words( C, "," );
Si vous convertissez une chaîne en liste de caractères uniques (un peu différent de la demande de OP mais correspond au titre), vous pouvez alors faire :
Items( "hello", "" );
Ce qui renvoie :
{"h", "e", "l", "l", "o"}