Questions from a "dummy" - page 35

 
Silent:

Yes, if you remove up to 3 letters

That's not the point.

Then we wait for a response from more competent comrades.))
 
tol64:
Try to study this or that topic as you need it. Once you get to a point in your system where you need certain "levers" (functions), then start experimenting with them. I found that this method of studying (in my case) works quite well))).
Yes, I do so, I can't get out of inite with input parameters :) and without arrays it will be quite hard to go further, tested on 4.
 
tol64:
Then we wait for a response from more competent comrades.))
Waiting :)
 
Silent:
Wait :)

Better yet, don't wait.) Keep experimenting.))

Remove the value from the array when declaring it. It must be like in the help:

string result[];
And you won't have any errors. As I understand it, the entire string specified in to_split variable is placed in the array, and then substring extraction goes on.
Документация по MQL5: Строковые функции / StringSubstr
Документация по MQL5: Строковые функции / StringSubstr
  • www.mql5.com
Строковые функции / StringSubstr - Документация по MQL5
 
tol64:

Better yet, don't wait.) Keep experimenting.))

Remove the value from the array when declaring it. It must be the way it is written in the help:

And there will be no errors. As far as I understood, the entire string specified in the to_split variable is placed in the array and then substring extraction is performed.

It's clear. Only this is a potential pitfall - I actually encountered this error in my code and it works - with an error.

For example, here is the help text for CopyBuffer

Если необходимо копировать заранее известное количество данных,
то лучше это делать в статически выделенный буфер,
чтобы избежать излишнего перевыделения памяти.

Where is the logic? I mean, what is the difference with StringSplit?

And if you make all arrays dynamic, you will run out of memory.

Upgr ArrayResize with zeroing is a variant, of course, but I don't like it.

 
tol64:

As I understand it, the entire string specified in the to_split variable is placed in the array, and then the substring is extracted.

No, it can't be like that.
 
Silent:

In your example, the problem is that the StringSplit() function itself takes care of the required amount of memory in the array passed as a parameter.

So it turns out that a static array works (if there is enough memory) but causes an error because people try to re-partition it.

Try the static and distributed dynamic arrays, and you'll see:

void OnStart()
  {
//---
   string to_split="мама_мыла_раму"; // строка для разбивки на подстроки
   string sep="_";                // раздедитель в виде символа
   ushort u_sep;                  // код символа разделителя
   string result[3];               // массив для получения строк
   //ArrayResize(result,3);    
//--- получим код разделителя
   u_sep=StringGetCharacter(sep,0);
   //--- разобьем строку на подстроки
   int k=StringSplit(to_split,u_sep,result);
   Print("GetLastError=",GetLastError()); // это я добавил чтоб видеть есть ли ошибка   
//--- выведем комментарий 
   PrintFormat("Получено строк: %d. Использован разделитель '%s' с кодом %d",k,sep,u_sep);
   //--- теперь выведем все полученные строки
   if(k>0)
     {
      for(int i=0;i<k;i++)
        {
         PrintFormat("result[%d]=%s",i,result[i]);
        }
     }
  }
Документация по MQL5: Основы языка / Типы данных / Объект динамического массива
Документация по MQL5: Основы языка / Типы данных / Объект динамического массива
  • www.mql5.com
Основы языка / Типы данных / Объект динамического массива - Документация по MQL5
 
Urain:

Try the variant with static and distributed dynamic arrays and it will become clear:

To try the dynamic array, just uncomment ArrayResize(result,3); ? If so, there will be a warning on compilation:

 
tol64:

To try a dynamic array, just uncomment ArrayResize(result,3); ? If so, there's a warning on compilation:


is this an unsolvable problem ? make it dynamic.
 
Urain:

In your example, the problem is that the StringSplit() function itself takes care of the required amount of memory in the array passed as a parameter.

This is why a static array works (if there is enough memory), but causes an error because people try to re-partition it.

So, can we use StringSplit() with static arrays or not?

If you can, there shouldn't be an error if there is enough memory. If not, it shouldn't work.

Reason: