Tuesday, June 28, 2011

Con2str and str2con functions in the Global Class

Many a times we use the functions which are available in the global class of AX.
2 important methods which we normally use during the import of text file or CSV file is con2str() and str2con() functions. Both these functions have limitations of use .

With respect to con2str it is that the string returned from the container cannot exceed more than 1000 char and with respect to the str2con the limitation is that chopps of the zero added before the string for example if you add str2con("00001,0002",",") then the entry in the container would be 1 and 2 respectively at position 1 and 2 of the container . This creates an issue where the Items in the system starts with the 0.

So I twisted both the functions slightly to get our desired o/p
i.e. String greater than 1000 char with con2str() method and
inclusion of 0 with str2con() method.

Here is what I changed :

static str con2strwithchange(container c, str 10 sep = ',')
{
    int idx = 0;
    int len = conlen(c);
    str tmp;
    str retStr;
    ;
   while (idx < len)
  {
       idx += 1;
      if (retStr)
      retStr += sep;
      tmp = conpeek(c,idx);
      retStr += tmp;
  }
  return retStr;
}

static container str2conChanged(str _value, str 10 _sep = ',')
{
      int length = strlen(_value);
      int i = 1;
      int j = strscan(_value, _sep, 1, length);
      container ret;
      void add2Ret(str _current)
      {
           // v-artemt, 26 Jul 2004, PS#: 1741
          //if (match('<:d+>', _current))
         // ret += str2int(_current);
         //else
         ret += _current;
       }
      ;
      while (j)
     {
          add2Ret(substr(_value, i, j-i));
          i = j+1;
         j = strscan(_value, _sep, i, length);
     }
     add2Ret(substr(_value, i, length-i+1));
     return ret;
}

No comments:

Post a Comment