date-fns format is not localizing numbers - localization

I want to convert a time string to Arabic. For example I want to convert 10:00 PM to مساءاً ١٠:٠٠. I am using format function with locale.
import ar from 'date-fns/locale/ar-SA'
...
str = format(date, 'h:mm a', {locale: ar})
But it gives a string like this 10:00 م. Basically digits are not getting translated.

You can try with this logic
const mappedDigit = (char: string): string => {
switch (char) {
case '0':
return '٠';
case '1':
return '١';
case '2':
return '٢';
case '3':
return '٣';
case '4':
return '٤';
case '5':
return '٥';
case '6':
return '٦';
case '7':
return '٧';
case '8':
return '٨';
case '9':
return '٩';
default:
return char;
}
};
const getArabicFormats = (englishString: string): string => {
let newArabicString = '';
for (let i = 0; i < englishString.length; i += 1) {
newArabicString += mappedDigit(englishString[i]);
}
newArabicString = newArabicString.replace('AM', 'ص');
newArabicString = newArabicString.replace('PM', 'م');
return newArabicString;
};

Related

How to set cell color based in duplicated value in column with different colours for each values

In a Google spreadsheet doc, I need to set the cell colour based in duplicated value in column with different colours for each duplicated values. The colour should be set to 'random' so that all of them will be different.
After the condition is applied the colour of the cells should look this: https://docs.google.com/spreadsheets/d/1YuUjg_PqD53AoTrxgvnEHYwZ_disqvNKDMmp5dRYq4I/edit?usp=sharing
This question is similar to this How to highlight cell if value duplicate in same column for google spreadsheet? but not the same since I need to give different colours for each duplicated value
I guess I might need a script to get this done? I don't think this could be done with 'conditional formatting' rule feature.
UPDATE:
The conditionals posted by Player0 works great. But the problem is that in the example I just posted a few cells. In the real spreadsheet I have hundreds of values which change from time to time. Then with this approach I will need to create a lot of condicional rules. I need to do this dynamically with a single formula. It should be a single formula/script to be applied to Column A which generate a random color for each value coincidence...
yellow:
=(ARRAYFORMULA(VLOOKUP(A1; {UNIQUE(A$1:A)\
ROW(INDIRECT("A1:A"&COUNTUNIQUE(A$1:A)+1))}; 2; 0))=1)*
(COUNTIF(A:A; A1)>1)
blue:
=(ARRAYFORMULA(VLOOKUP(A1; {UNIQUE(A$1:A)\
ROW(INDIRECT("A1:A"&COUNTUNIQUE(A$1:A)+1))}; 2; 0))=2)*
(COUNTIF(A:A; A1)>1)
pink:
=(ARRAYFORMULA(VLOOKUP(A1; {UNIQUE(A$1:A)\
ROW(INDIRECT("A1:A"&COUNTUNIQUE(A$1:A)+1))}; 2; 0))=3)*
(COUNTIF(A:A; A1)>1)
green:
=(ARRAYFORMULA(VLOOKUP(A1; {UNIQUE(A$1:A)\
ROW(INDIRECT("A1:A"&COUNTUNIQUE(A$1:A)+1))}; 2; 0))=4)*
(COUNTIF(A:A; A1)>1)
red:
=(ARRAYFORMULA(VLOOKUP(A1; {UNIQUE(A$1:A)\
ROW(INDIRECT("A1:A"&COUNTUNIQUE(A$1:A)+1))}; 2; 0))=5)*
(COUNTIF(A:A; A1)>1)
orange:
=(ARRAYFORMULA(VLOOKUP(A1; {UNIQUE(A$1:A)\
ROW(INDIRECT("A1:A"&COUNTUNIQUE(A$1:A)+1))}; 2; 0))=6)*
(COUNTIF(A:A; A1)>1)
Here is a script that works:
function colorDuplicates() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
var column = 1
ss.getRange(2, column, lr).setBackground(null);
var color = ["#EA9999","#F9CB9C","#FFE599","#B6D7A8","#A2C4C9","#9FC5E8","#B4A7D6","#D5A6BD","#CCCCCC","#B45F06","#666666","#FF0000","#FF9900","#FFFF00","#00FF00","#00FFFF"];
var c = 0;
var checkcolor = false;
for (var i = 2; i < lr+1;i++){
if (checkcolor == true) {
c++;
checkcolor = false;
}
var a = ss.getRange(i, column).getValue();
if (a == "") {continue;}
var cellcolor = ss.getRange(i, column).getBackground();
if (cellcolor != "#ffffff") {continue;}
for (var j = i+1;j< lr+1;j++){
var b = ss.getRange(j, column).getValue();
if (a != b) {continue;}
var cellcolor = ss.getRange(j, column).getBackground();
if (cellcolor != "#ffffff") {continue;}
ss.getRange(i, column).setBackground(color[c]);
ss.getRange(j, column).setBackground(color[c]);
checkcolor = true;
}
}
}
function colorDuplicates2() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = getLastRowSpecial(ss.getRange("A:A").getValues());
var lc = ss.getLastColumn();
ss.getRange(2, 1, lr).setBackground(null);
ss.insertColumnAfter(lc);
ss.getRange(1, lc+1).setFormula("=FILTER(UNIQUE(A2:A);UNIQUE(A2:A)<>\"\")"); // Change the ";" for a "," if you are in the us
var numValues = getLastRowSpecial(ss.getRange(1, lc+1, lr).getValues());
var values = new Array(36);
for (var a = 1;a<numValues+1;a++){
values[a] = String(ss.getRange(a, lc+1).getValue());
}
var color = ["#980000", "#ff0000", "#ff9900", "#ffff00", "#00ff00", "#00ffff", "#4a86e8", "#0000ff", "#9900ff", "#ff00ff", "#e6b8af", "#f4cccc", "#fce5cd", "#ea9999", "#f9cb9c", "#ffe599", "#b6d7a8", "#a2c4c9", "#a4c2f4", "#9fc5e8", "#b4a7d6", "#d5a6bd", "#cc4125", "#e06666", "#f6b26b", "#ffd966", "#93c47d", "#76a5af", "#6d9eeb", "#6fa8dc", "#8e7cc3", "#c27ba0", "#a61c00", "#cc0000", "#e69138", "#f1c232", "#6aa84f", "#45818e", "#3c78d8", "#3d85c6", "#674ea7", "#a64d79", "#85200c", "#990000"];
for (var i = 2;i<lr+1;i++){
switch (String(ss.getRange(i, 1).getValue())) {
case values[1]:
ss.getRange(i, 1).setBackground(color[1]);
break;
case values[2]:
ss.getRange(i, 1).setBackground(color[2]);
break;
case values[3]:
ss.getRange(i, 1).setBackground(color[3]);
break;
case values[4]:
ss.getRange(i, 1).setBackground(color[4]);
break;
case values[5]:
ss.getRange(i, 1).setBackground(color[5]);
break;
case values[6]:
ss.getRange(i, 1).setBackground(color[6]);
break;
case values[7]:
ss.getRange(i, 1).setBackground(color[7]);
break;
case values[8]:
ss.getRange(i, 1).setBackground(color[8]);
break;
case values[9]:
ss.getRange(i, 1).setBackground(color[9]);
break;
case values[10]:
ss.getRange(i, 1).setBackground(color[10]);
break;
case values[11]:
ss.getRange(i, 1).setBackground(color[11]);
break;
case values[12]:
ss.getRange(i, 1).setBackground(color[12]);
break;
case values[13]:
ss.getRange(i, 1).setBackground(color[13]);
break;
case values[14]:
ss.getRange(i, 1).setBackground(color[14]);
break;
case values[15]:
ss.getRange(i, 1).setBackground(color[15]);
break;
case values[16]:
ss.getRange(i, 1).setBackground(color[16]);
break;
case values[17]:
ss.getRange(i, 1).setBackground(color[17]);
break;
case values[18]:
ss.getRange(i, 1).setBackground(color[18]);
break;
case values[19]:
ss.getRange(i, 1).setBackground(color[19]);
break;
case values[20]:
ss.getRange(i, 1).setBackground(color[20]);
break;
case values[21]:
ss.getRange(i, 1).setBackground(color[21]);
break;
case values[22]:
ss.getRange(i, 1).setBackground(color[22]);
break;
case values[23]:
ss.getRange(i, 1).setBackground(color[23]);
break;
case values[24]:
ss.getRange(i, 1).setBackground(color[24]);
break;
case values[25]:
ss.getRange(i, 1).setBackground(color[25]);
break;
case values[26]:
ss.getRange(i, 1).setBackground(color[26]);
break;
case values[27]:
ss.getRange(i, 1).setBackground(color[27]);
break;
case values[28]:
ss.getRange(i, 1).setBackground(color[28]);
break;
case values[29]:
ss.getRange(i, 1).setBackground(color[29]);
break;
case values[30]:
ss.getRange(i, 1).setBackground(color[30]);
break;
case values[31]:
ss.getRange(i, 1).setBackground(color[31]);
break;
case values[32]:
ss.getRange(i, 1).setBackground(color[32]);
break;
case values[33]:
ss.getRange(i, 1).setBackground(color[33]);
break;
case values[34]:
ss.getRange(i, 1).setBackground(color[34]);
break;
case values[35]:
ss.getRange(i, 1).setBackground(color[35]);
break;
case values[36]:
ss.getRange(i, 1).setBackground(color[36]);
break;
}
}
ss.deleteColumn(lc+1);
}
function getLastRowSpecial(range){
var rowNum = 0;
var blank = false;
for(var row = 0; row < range.length; row++){
if(range[row][0] === "" && !blank){
rowNum = row;
blank = true;
}else if(range[row][0] !== ""){
blank = false;
};
};
return rowNum;
}
It has only 16 colors, and it will leave blank the duplicates that finds after that, but you can add as many more colors as you need.
It works for finding duplicates in column A, but you can also change that if you need it to be another column.
Try it and feel free to ask me anything. Good luck!
*I edited my previous answer beacuse it didnt take in to account values that appeard more than twice.
*I edited again to avoid empty cells.
You can use a script function to take advantage of a hashing algorithm like md5 and obtain an (almost) unique byte string for any cell value.
Selecting the first 3 bytes and adding a leading "#", you can convert such a hash into a hex color.
After that, looping over the range values to determine the color codes and setting the background is all that is required.
/**
* Return an md5 hash as hex string.
* #example md5("lorem ipsum") => "80a751fde577028640c419000e33eba6"
*/
function md5(txt) {
return Utilities.computeDigest(
Utilities.DigestAlgorithm.MD5,
txt
).map(
c => (255 & c).toString(16).padStart(2, '0')
).join(
""
);
}
/**
* Return a hex color from any string.
* #example txt2color("lorem ipsum") => "#80a751"
*/
function txt2color(txt) {
return "#" + md5(txt).slice(0, 6);
}
/**
* Set the background color of the cells in the active
* range so that the cells with same value get the
* same background color
*/
function highlight_same_values() {
const range = SpreadsheetApp.getActiveRange();
const bgcolors = range.getValues().map(
row => row.map(
cell => cell ? txt2color(cell) : null
)
);
range.setBackgrounds(bgcolors);
}

LINQ expression String.Contains doesn't work

I have a Kendo Grid, which has to be filtered by a column. It works fine with IsEqualTo, IsNotEqualTo, StartsWith and EndsWith cases but not with Contains or DoesNotContain cases.
My code is like:
switch (ertesitettDescriptor.Operator)
{
case FilterOperator.IsEqualTo:
res = res.Where(m => m.ErtesitendoSzerv.Nev.ToLower() == filterValue.ToLower());
break;
case FilterOperator.IsNotEqualTo:
res = res.Where(m => !(m.ErtesitendoSzerv.Nev.ToLower() == filterValue.ToLower()));
break;
case FilterOperator.Contains:
res = res.Where(m => m.ErtesitendoSzerv.Nev.ToLower().Contains(filterValue.ToLower()));
break;
case FilterOperator.DoesNotContain:
res = res.Where(m => !(m.ErtesitendoSzerv.Nev.ToLower().Contains(filterValue.ToLower())));
break;
case FilterOperator.StartsWith:
res = res.Where(m => (m.ErtesitendoSzerv.Nev.ToLower().StartsWith(filterValue.ToLower())));
break;
case FilterOperator.EndsWith:
res = res.Where(m => (m.ErtesitendoSzerv.Nev.ToLower().EndsWith(filterValue.ToLower())));
break;
default:
return new JavaScriptResult { Script = "kendo.alert('A szűrés módja nem támogatott!')" };
}
All works fine, so the values are ok, but the .Contains method doesn't give back any records.
If you are using LINQ to SQL, it automatically ignores case sensitivity. You can achieve results without converting it to lowercase or uppercase.
res = res.Where(m => !(m.ErtesitendoSzerv.Nev.Contains(filterValue)));
Just took the liberty of cleaning up the code alittle, and added StringComparison to your filtering. i assume the StringComparison solves your issue, as you no longer need to use ToLower().
StringComparison comp = StringComparison.OrdinalIgnoreCase;
switch (ertesitettDescriptor.Operator)
{
case FilterOperator.IsEqualTo:
res = res.Where(m => m.ErtesitendoSzerv.Nev.Equals(filterValue, comp);
break;
case FilterOperator.IsNotEqualTo:
res = res.Where(m => !m.ErtesitendoSzerv.Nev.Equals(filterValue, comp);
break;
case FilterOperator.Contains:
res = res.Where(m => m.ErtesitendoSzerv.Nev.Contains(filterValue, comp));
break;
case FilterOperator.DoesNotContain:
res = res.Where(m => !m.ErtesitendoSzerv.Nev.Contains(filterValue, comp));
break;
case FilterOperator.StartsWith:
res = res.Where(m => m.ErtesitendoSzerv.Nev.StartsWith(filterValue, comp));
break;
case FilterOperator.EndsWith:
res = res.Where(m => m.ErtesitendoSzerv.Nev.EndsWith(filterValue, comp));
break;
default:
return new JavaScriptResult { Script = "kendo.alert('A szűrés módja nem támogatott!')" };
}

What is the "*all* format in lua file:read() means?

I maintain some old code written in LUA, there are some snippet I could not understand,
local f = io.open("someFile.lua", "r");
local szFileContent = "return {};";
if f then
szFileContent = f:read("*all");
f:close();
end
The format used in read function is something weird, I see the format *a, and *l in lua51 manual https://www.lua.org/manual/5.1/manual.html#pdf-file:read,
but not the *all format
in the function read from liolib.c only the first two ('*' and 'a') characters are checked, the rest of the string is ignored:
// ...
const char *p = lua_tostring(L, n);
luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
switch (p[1]) {
case 'n': /* number */
success = read_number(L, f);
break;
case 'l': /* line */
success = read_line(L, f);
break;
case 'a': /* file */
read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */
success = 1; /* always success */
break;
default:
return luaL_argerror(L, n, "invalid format");
//...

How to stop Dart's .forEach()?

List data = [1, 2, 3];
data.forEach((value) {
if (value == 2) {
// how to stop?
}
print(value);
});
I tried return false; which works in jQuery, but it does not work in Dart.
Is there a way to do it?
You can also use a for/in, which implicitly uses the iterator aptly demonstrated in the other answer:
List data = [1,2,3];
for(final i in data){
print('$i');
if (i == 2){
break;
}
}
It is also possible to implement your example using forEach() and takeWhile().
var data = [1, 2, 3];
data.takeWhile((val) => val != 2).forEach(print);
Breaking a List
List<int> example = [ 1, 2, 3 ];
for (int value in example) {
if (value == 2) {
break;
}
}
Breaking a Map
If you're dealing with a Map you can't simply get an iterator from the given map, but you can still use a for by applying it to either the values or the keys. Since you sometimes might need the combination of both keys and values, here's an example:
Map<String, int> example = { 'A': 1, 'B': 2, 'C': 3 };
for (String key in example.keys) {
if (example[key] == 2 && key == 'B') {
break;
}
}
Note that a Map doesn't necessarily have they keys as [ 'A', 'B', 'C' ] use a LinkedHashMap if you want that. If you just want the values, just do example.values instead of example.keys.
Alternatively if you're only searching for an element, you can simplify everything to:
List<int> example = [ 1, 2, 3 ];
int matched = example.firstMatching((e) => e == 2, orElse: () => null);
The callback that forEach takes returns void so there is no mechanism to stop iteration.
In this case you should be using iterators:
void listIteration() {
List data = [1,2,3];
Iterator i = data.iterator;
while (i.moveNext()) {
var e = i.current;
print('$e');
if (e == 2) {
break;
}
}
}
Dart does not support non-local returns, so returning from a callback won't break the loop.
The reason it works in jQuery is that each() checks the value returned by the callback.
Dart forEach callback returns void.
http://docs.jquery.com/Core/each
based on Greg Lowe post, I used where for my project and also it works.
var data = [1, 2, 3];
data.where((val) => val != 2).forEach(print);
Using Multiple Loop
Break Outer Loop
OUTER: for (var i = 0; i < m.length; i++) {
for (var j = 0; j < m[i].length; j++) {
if (m[i][j] < 0) {
print("Negative value found at $i,$j: ${m[i][j]}");
break OUTER;
}
}
}
Continue Outer Loop
outer: for (var v in a) {
for (var w in b) {
if (w == v) continue outer;
}
print(v);
}
Here is a full sample by for-in loop, that close to forEach style.
void main(){
var myList = [12, 18, 24, 63, 84,99];
myList.forEach((element) {
print(element);
if (element ==24); //break; // does not work
});
for(var element in myList) {
print(element);
if (element==24) break;
}
}
Somebody suggest where() but it is not a general replacement for forEach() with break capability
(where is however a correct replacement for the use case showed in the example of the question. I, on the other hand, focus on the question in the title)
The functionality of foreach() but with an equivalent of break, is given by any(): to continue the loop you return false, to stop you return true; the result of any() can be ignored. I think it is more similar to each() in jquery (but in dart to stop you return true).
To have a loop with the index, but also the possibility in case of break the loop, I use the following extension:
extension IterableUtils<E> on Iterable<E> {
/**
Similar to Iterable.forEach() but:
- with an index argument
- with the optional capacity to break the loop, returning false
Note: as for the return clause, you can omit it, as with forEach()
*/
void forEachIndexed(Function(E element, int index) f) {
int index = 0;
for (E element in this) {
if (f(element, index) == false) break;
index++;
}
}
}
Example:
void main() {
List list = ["a", "b", "c"];
list.forEachIndexed((element, index) {
print("$index: $element");
//Optional:
if (element == "b") return false; //break
});
}
You CAN empty return from a forEach to break the loop;
List<int> data = [1, 2, 3];
int _valueToBePrinted;
data.forEach((value) {
if (value == 2) {
_valueToBePrinted = value;
return;
}
});
// you can return something here to
// return _valueToBePrinted;
print(value);
anyway you shouldn't...
the catch is, you can't return anything in the entire forEach loop
//This don't work
data.forEach((value) {
if (value == 2) {
_valueToBePrinted = value;
return;
}
if (value == 1) {
return value;
}
});

String Split in DXL

I have a string
Ex: "We prefer questions that can be answered; not just discussed "
now i want to split this string from ";"
like
We prefer questions that can be answered
and
not just discussed
is this possible in DXL.
i am learning DXL, so i don't have any idea whether we can split or not.
Note : This is not a home work.
I'm sorry for necroing this post. Being new to DXL I spent some time with the same challenge. I noticed that the implementations available on the have different specifications of "splitting" a string. Loving the Ruby language, I missed an implementation which comes at least close to the Ruby version of String#split.
Maybe my findings will be helpful to anybody.
Here's a functional comparison of
Variant A: niol's implementation (which at a first glance, appears to be the same implementation which is usually found at Capri Soft,
Variant B: PJT's implementation,
Variant C: Brett's implementation and
Variant D: my implementation (which provides the correct functionality imo).
To eliminate structural difference, all implementations were implemented in functions, returning a Skip list or an Array.
Splitting results
Note that all implementations return different results, depending on their definition of "splitting":
string mellow yellow; delimiter ello
splitVariantA returns 1 elements: ["mellow yellow" ]
splitVariantB returns 2 elements: ["m" "llow yellow" ]
splitVariantC returns 3 elements: ["w" "w y" "" ]
splitVariantD returns 3 elements: ["m" "w y" "w" ]
string now's the time; delimiter
splitVariantA returns 3 elements: ["now's" "the" "time" ]
splitVariantB returns 2 elements: ["" "now's the time" ]
splitVariantC returns 5 elements: ["time" "the" "" "now's" "" ]
splitVariantD returns 3 elements: ["now's" "the" "time" ]
string 1,2,,3,4,,; delimiter ,
splitVariantA returns 4 elements: ["1" "2" "3" "4" ]
splitVariantB returns 2 elements: ["1" "2,,3,4,," ]
splitVariantC returns 7 elements: ["" "" "4" "3" "" "2" "" ]
splitVariantD returns 7 elements: ["1" "2" "" "3" "4" "" "" ]
Timing
Splitting the string 1,2,,3,4,, with the pattern , for 10000 times on my machine gives these timings:
splitVariantA() : 406 ms
splitVariantB() : 46 ms
splitVariantC() : 749 ms
splitVariantD() : 1077 ms
Unfortunately, my implementation D is the slowest. Surprisingly, the regular expressions implementation C is pretty fast.
Source code
// niol, modified
Array splitVariantA(string splitter, string str){
Array tokens = create(1, 1);
Buffer buf = create;
int str_index;
buf = "";
for(str_index = 0; str_index < length(str); str_index++){
if( str[str_index:str_index] == splitter ){
array_push_str(tokens, stringOf(buf));
buf = "";
}
else
buf += str[str_index:str_index];
}
array_push_str(tokens, stringOf(buf));
delete buf;
return tokens;
}
// PJT, modified
Skip splitVariantB(string s, string delimiter) {
int offset
int len
Skip skp = create
if ( findPlainText(s, delimiter, offset, len, false)) {
put(skp, 0, s[0 : offset -1])
put(skp, 1, s[offset +1 :])
}
return skp
}
// Brett, modified
Skip splitVariantC (string s, string delim) {
Skip skp = create
int i = 0
Regexp split = regexp "^(.*)" delim "(.*)$"
while (split s) {
string temp_s = s[match 1]
put(skp, i++, s[match 2])
s = temp_s
}
put(skp, i++, s[match 2])
return skp
}
Skip splitVariantD(string str, string pattern) {
if (null(pattern) || 0 == length(pattern))
pattern = " ";
if (pattern == " ")
str = stringStrip(stringSqueeze(str, ' '));
Skip result = create;
int i = 0; // index for searching in str
int j = 0; // index counter for result array
bool found = true;
while (found) {
// find pattern
int pos = 0;
int len = 0;
found = findPlainText(str[i:], pattern, pos, len, true);
if (found) {
// insert into result
put(result, j++, str[i:i+pos-1]);
i += pos + len;
}
}
// append the rest after last found pattern
put(result, j, str[i:]);
return result;
}
Quick join&split I could come up with. Seams to work okay.
int array_size(Array a){
int size = 0;
while( !null(get(a, size, 0) ) )
size++;
return size;
}
void array_push_str(Array a, string str){
int array_index = array_size(a);
put(a, str, array_index, 0);
}
string array_get_str(Array a, int index){
return (string get(a, index, 0));
}
string str_join(string joiner, Array str_array){
Buffer joined = create;
int array_index = 0;
joined += "";
for(array_index = 0; array_index < array_size(str_array); array_index++){
joined += array_get_str(str_array, array_index);
if( array_index + 1 < array_size(str_array) )
joined += joiner;
}
return stringOf(joined)
}
Array str_split(string splitter, string str){
Array tokens = create(1, 1);
Buffer buf = create;
int str_index;
buf = "";
for(str_index = 0; str_index < length(str); str_index++){
if( str[str_index:str_index] == splitter ){
array_push_str(tokens, stringOf(buf));
buf = "";
}else{
buf += str[str_index:str_index];
}
}
array_push_str(tokens, stringOf(buf));
delete buf;
return tokens;
}
If you only split the string once this is how I would do it:
string s = "We prefer questions that can be answered; not just discussed"
string sub = ";"
int offset
int len
if ( findPlainText(s, sub, offset, len, false)) {
/* the reason why I subtract one and add one is to remove the delimiter from the out put.
First print is to print the prefix and then second is the suffix.*/
print s[0 : offset -1]
print s[offset +1 :]
} else {
// no delimiter found
print "Failed to match"
}
You could also use regular expressions refer to the DXL reference manual. It would be better to use regular expressions if you want to split up the string by multiple delimiters such as str = "this ; is an;example"
ACTUALLY WORKS:
This solution will split as many times as needed, or none, if the delimiter doesn't exist in the string.
This is what I have used instead of a traditional "split" command.
It actually skips the creation of an array, and just loops through each string that would be in the array and calls "someFunction" on each of those strings.
string s = "We prefer questions that can be answered; not just discussed"
// for this example, ";" is used as the delimiter
Regexp split = regexp "^(.*);(.*)$"
// while a ";" exists in s
while (split s) {
// save the text before the last ";"
string temp_s = s[match 1]
// call someFunction on the text after the last ";"
someFunction(s[match 2])
// remove the text after the last ";" (including ";")
s = temp_s
}
// call someFunction again for the last (or only) string
someFunction(s)
Sorry for necroing an old post; I just didn't find the other answers useful.
Perhaps someone would find handy this fused solution as well. It splits string in Skip, based on delimiter, which can actually have length more then one.
Skip splitString(string s1, string delimit)
{
int offset, len
Skip splited = create
while(findPlainText(s1, delimit, offset, len, false))
{
put(splited, s1[0:offset-1], s1[0:offset-1])
s1 = s1[offset+length(delimit):length(s1)-1]
}
if(length(s1)>0)
{
put (splited, s1, s1)
}
return splited
}
I tried this out and worked out for me...
string s = "We prefer questions that can be answered,not just discussed,hiyas"
string sub = ","
int offset
int len
string s1=s
while(length(s1)>0){
if ( findPlainText(s1, sub, offset, len, false)) {
print s1[0 : offset -1]"\n"
s1= s1[offset+1:length(s1)]
}
else
{
print s1
s1=""
}
}
Here is a better implementation. This is a recursive split of the string by searching a keyword.
pragma runLim, 10000
string s = "We prefer questions that can be answered,not just discussed,hiyas;
Next Line,Var1,Nemesis;
Next Line,Var2,Nemesis1;
Next Line,Var3,Nemesis2;
New,Var4,Nemesis3;
Next Line,Var5,Nemesis4;
New,Var5,Nemesis5;"
string sub = ","
int offset
int len
string searchkey=null
string curr=s
string nxt=s
string searchline=null
string Modulename=""
string Attributename=""
string Attributevalue=""
while(findPlainText(curr,"Next Line", offset,len,false))
{
int intlen=offset
searchkey=curr[offset:length(curr)]
if(findPlainText(searchkey,"Next Line",offset,len,false))
{
curr=searchkey[offset+1:length(searchkey)]
}
if(findPlainText(searchkey,";",offset,len,false))
{
searchline=searchkey[0:offset]
}
int counter=0
while(length(searchline)>0)
{
if (findPlainText(searchline, sub, offset, len, false))
{
if(counter==0)
{
Modulename=searchline[0 : offset -1]
counter++
}
else if(counter==1)
{
Attributename=searchline[0 : offset -1]
counter++
}
searchline= searchline[offset+1:length(searchline)]
}
else
{
if(counter==2)
{
Attributevalue=searchline[0:length(searchline)-2]
counter++
}
searchline=""
}
}
print "Modulename="Modulename " Attributename=" Attributename " Attributevalue= "Attributevalue "\n"
}

Resources