Ciao,
Esiste una funzione incorporata per convertire una stringa in un elenco? Ad esempio, ho una stringa "abcd1234cdef5678" e voglio che venga convertita in {"abcd", "1234", "cdef, "5678""}.
Grazie!
Ahmed
Un'altra tecnica:
parts = {};
rc = Pat Match( "1234567890abcdefghijklmnopqrstuv", Pat Repeat( Pat Len( 4 ) >> parts[N Items( parts ) + 1] ) );
Show( parts );
parti = {"1234", "5678", "90ab", "cdef", "ghij", "klmn", "opqr", "stuv"};
L'approccio di Jim è migliore per le stringhe brevi perché la prossima persona che mantiene il JSL lo capirà. Per stringhe di circa 10K, il pattern matching sarà notevolmente più veloce.
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 stringIl pattern match (curva rossa) evita di rielaborare la stringa
È il JSL per creare il grafico...
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( "" )} )
)
);
Il motivo per cui l'esempio del ciclo for rallenta su stringhe di grandi dimensioni: la funzione substr deve trovare la posizione di ogni sottostringa, a partire da inizio . La corrispondenza del modello si sposta attraverso la stringa una volta .
Non sono a conoscenza di una tale funzione, ma potresti usare questo codice o renderlo una funzione
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 qui è come una funzione</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");
Ciao Jim,
Come ti avvicineresti a questo se hai una stringa e una virgola è il punto in cui vuoi smettere di separare la stringa.
Per esempio,
convertendo questo " C = "Piove molto, molto forte oggi" in b = {"Sta piovendo", "molto forte oggi" }
Grazie!
Aggiornamento:
Ho trovato un thread più vecchio a riguardo da Jeff_Perkinson.
Questo è il collegamento Risolto: utilizzo di più caratteri come delimitatori per la funzione Parole - Comunità utenti JMP
La soluzione che ha funzionato per me è stata usare questo metodo:
words(substitute("Test1eeTest2", "ee", "!"),"!")
/*:
{"Test1", "Test2"}
<BR />Grazie.
-Amico
Utilizza la funzione Words() per dividere la stringa in una Lista con gli elementi di ciascuna delle sottostringhe identificate dal delimitatore specificato, nel tuo caso una virgola.
names default to here(1);
C = "It is, raining, very, hard today";
b = Words( C, "," );
Se si converte una stringa in un elenco di singoli caratteri (leggermente diverso dalla richiesta di OP ma corrisponde al titolo), è possibile eseguire:
Items( "hello", "" );
Che restituisce:
{"h", "e", "l", "l", "o"}