Subscribe for automatic updates: RSS icon RSS

Login icon Sign in for full access | Help icon Help
Advanced search

Pages: [1]
  Reply  |  Print  
Author Topic: How to trim a specific part of a characters  (Read 7921 times)
Abdulrahman A.
Posts: 4


« on: July 24, 2016, 10:05:32 pm »

I have the following example for a Character 'g_temp' with an Array 'g_arr' :-

Code
  1. Define g_temp CHAR(1000),
  2. Array g_arr    OF RECORD
  3. r_code char(3),
  4. r_desc char(100),
  5. r_date date
  6. END RECORD
  7.  
  8. Main
  9. Let g_temp = "<<<<1<<ABC<<<<2<<TESTING<<<<3<<10/MAY/2016<<<<1<<ZXY<<<<2<<LOOK AT THIS<<<<3<<31/AUG/2016<<<<1<<REG<<<<2<<NICE ONE<<<<3<<04/NOV/2016"
  10. End Main

I would like to get some help in separating this variable contents as follow:

To place the first part of what is between <<<<1<< and <<<<2<< into g_arr. r_code [n]
To place the second part of what is between <<<<2<< and <<<<3<< into g_arr. r_desc [n]
To place the third part of what is between <<<<3<< and <<<<1<< into g_arr. r_date [n]
and so on

Which will end up the following results for our provided 'g_temp' example:

Code
  1. g_arr. r_code [1] = ABC
  2. g_arr. r_desc [1] = TESTING
  3. g_arr. r_date [1] = 10/MAY/2016
  4. g_arr. r_code [2] = ZXY
  5. g_arr. r_desc [2] = LOOK AT THIS
  6. g_arr. r_date [2] = 31/AUG/2016
  7. g_arr. r_code [3] = REG
  8. g_arr. r_desc [3] = NICE ONE
  9. g_arr. r_date [3] = 04/NOV/2016
Stefan S.
Posts: 84


« Reply #1 on: July 25, 2016, 08:46:40 am »

Hi you can use the StringTokenizer to separate your string in a single-Column Array
and read this array in your array.
Maybe there is a easier way, but it works:

Code
  1. define g_temp string
  2.  
  3. define g_arr dynamic array of record
  4.          r_code char(3),
  5.          r_desc char(100),
  6.          r_date date
  7.       end record
  8.  
  9. define g_tmparray dynamic array of record
  10.          tmpfield string
  11.       end record,
  12.       g_tmpfield string
  13.  
  14. define g_token base.StringTokenizer,
  15.       g_z1    smallint,
  16.       g_z2    smallint,
  17.       g_z3    smallint
  18.  
  19. main
  20.  
  21.   let g_temp = "<<<<1<<ABC<<<<2<<TESTING<<<<3<<10/MAY/2016<<<<1<<ZXY<<<<2<<LOOK AT THIS<<<<3<<31/AUG/2016<<<<1<<REG<<<<2<<NICE ONE<<<<3<<04/NOV/2016"
  22.  
  23.   call g_arr.clear()
  24.   call g_tmparray.clear()
  25.   let g_z1 = 0
  26.   let g_z3 = 0
  27.  
  28.   let g_token = base.StringTokenizer.create(g_temp, "<<<<")
  29.  
  30.   while g_token.hasMoreTokens()
  31.      let g_tmpfield = g_token.nextToken()
  32.  
  33.      case g_tmpfield     --remove your "string counter" before inserted in the tmparray
  34.      when 1
  35.         continue while
  36.      when 2
  37.         continue while
  38.      when 3
  39.         continue while
  40.      end case
  41.  
  42.      let g_z1 = g_z1 + 1
  43.      let g_tmparray[g_z1].tmpfield = g_tmpfield
  44.   end while
  45.  
  46.   --show the tmparray
  47.   for g_z1 = 1 to g_tmparray.getLength()
  48.      display g_tmparray[g_z1].*
  49.   end for
  50.  
  51.   --now build your g_arr from the tmparray
  52.   let g_z2 = 1
  53.   for g_z1 = 1 to g_tmparray.getLength()
  54.      if g_z2 = 1 then
  55.         let g_z2 = g_z2 + 1
  56.         let g_z3 = g_z3 + 1
  57.         let g_arr[g_z3].r_code = g_tmparray[g_z1].tmpfield
  58.         continue for
  59.      end if
  60.  
  61.      if g_z2 = 2 then
  62.         let g_z2 = g_z2 + 1
  63.         let g_arr[g_z3].r_desc = g_tmparray[g_z1].tmpfield
  64.         continue for
  65.      end if
  66.  
  67.      if g_z2 = 3 then
  68.         let g_z2 = 1 --imporant
  69.         let g_arr[g_z3].r_date = g_tmparray[g_z1].tmpfield
  70.         continue for
  71.      end if
  72.   end for
  73.  
  74.   --result
  75.   for g_z1 = 1 to g_arr.getLength()
  76.      display g_arr[g_z1].r_code, "   ",  g_arr[g_z1].r_desc, "   ",  g_arr[g_z1].r_date
  77.   end for
  78.  
  79. end main
  80.  
  81.  


HTH Stefan

Abdulrahman A.
Posts: 4


« Reply #2 on: July 25, 2016, 12:17:18 pm »

Thank you for your clear explanation ^_^

Hi you can use the StringTokenizer to separate your string in a single-Column Array
and read this array in your array.
Maybe there is a easier way, but it works:

Code
  1. define g_temp string
  2.  
  3. define g_arr dynamic array of record
  4.          r_code char(3),
  5.          r_desc char(100),
  6.          r_date date
  7.       end record
  8.  
  9. define g_tmparray dynamic array of record
  10.          tmpfield string
  11.       end record,
  12.       g_tmpfield string
  13.  
  14. define g_token base.StringTokenizer,
  15.       g_z1    smallint,
  16.       g_z2    smallint,
  17.       g_z3    smallint
  18.  
  19. main
  20.  
  21.   let g_temp = "<<<<1<<ABC<<<<2<<TESTING<<<<3<<10/MAY/2016<<<<1<<ZXY<<<<2<<LOOK AT THIS<<<<3<<31/AUG/2016<<<<1<<REG<<<<2<<NICE ONE<<<<3<<04/NOV/2016"
  22.  
  23.   call g_arr.clear()
  24.   call g_tmparray.clear()
  25.   let g_z1 = 0
  26.   let g_z3 = 0
  27.  
  28.   let g_token = base.StringTokenizer.create(g_temp, "<<<<")
  29.  
  30.   while g_token.hasMoreTokens()
  31.      let g_tmpfield = g_token.nextToken()
  32.  
  33.      case g_tmpfield     --remove your "string counter" before inserted in the tmparray
  34.      when 1
  35.         continue while
  36.      when 2
  37.         continue while
  38.      when 3
  39.         continue while
  40.      end case
  41.  
  42.      let g_z1 = g_z1 + 1
  43.      let g_tmparray[g_z1].tmpfield = g_tmpfield
  44.   end while
  45.  
  46.   --show the tmparray
  47.   for g_z1 = 1 to g_tmparray.getLength()
  48.      display g_tmparray[g_z1].*
  49.   end for
  50.  
  51.   --now build your g_arr from the tmparray
  52.   let g_z2 = 1
  53.   for g_z1 = 1 to g_tmparray.getLength()
  54.      if g_z2 = 1 then
  55.         let g_z2 = g_z2 + 1
  56.         let g_z3 = g_z3 + 1
  57.         let g_arr[g_z3].r_code = g_tmparray[g_z1].tmpfield
  58.         continue for
  59.      end if
  60.  
  61.      if g_z2 = 2 then
  62.         let g_z2 = g_z2 + 1
  63.         let g_arr[g_z3].r_desc = g_tmparray[g_z1].tmpfield
  64.         continue for
  65.      end if
  66.  
  67.      if g_z2 = 3 then
  68.         let g_z2 = 1 --imporant
  69.         let g_arr[g_z3].r_date = g_tmparray[g_z1].tmpfield
  70.         continue for
  71.      end if
  72.   end for
  73.  
  74.   --result
  75.   for g_z1 = 1 to g_arr.getLength()
  76.      display g_arr[g_z1].r_code, "   ",  g_arr[g_z1].r_desc, "   ",  g_arr[g_z1].r_date
  77.   end for
  78.  
  79. end main
  80.  
  81.  


HTH Stefan


Reuben B.
Four Js
Posts: 949


« Reply #3 on: July 25, 2016, 11:43:40 pm »

I am not sure if stringTokenizor http://4js.com/online_documentation/fjs-fgl-manual-html/#c_fgl_ClassStringTokenizer.html is the best here.  It is designed for cases where the delimiter is constant between fields such as the case with CSV or Informix unload files.

So if you had "," as the delimiter, then you would have something like ...

Code
  1. Define g_temp CHAR(1000),
  2. Array g_arr    OF RECORD
  3. r_code char(3),
  4. r_desc char(100),
  5. r_date date
  6. END RECORD
  7. DEFINE tok base.StringTokenizer
  8. DEFINE idx INTEGER
  9.  
  10. Main
  11. Let g_temp = "ABC,TESTING,10/MAY/2016,ZXY,LOOK AT THIS,31/AUG/2016,REG,NICE ONE,04/NOV/2016"
  12. LET tok = base.StringTokenizer.create(g_temp,",")
  13. LET idx = 0
  14. WHILE tok.hasMoreTokens()
  15.   LET idx = idx + 1
  16.   LET g_arr[idx].r_code = tok.nextToken()
  17.   LET g_arr[idx].r_desc = tok.nextToken()
  18.   LET g_arr[idx].r_date = tok.nextToken()
  19. WHILE
  20. End Main

If the delimiter between each field was different as in your example, I'd be using base.String methods http://4js.com/online_documentation/fjs-fgl-manual-html/#c_fgl_datatypes_STRING_methods.html

Code
  1. Define g_temp STRING,
  2. Array g_arr    OF RECORD
  3. r_code char(3),
  4. r_desc char(100),
  5. r_date date
  6. END RECORD
  7. DEFINE idx INTEGER
  8. DEFINE pos1a, pos2,pos3,pos1b INTEGER
  9.  
  10. Main
  11. Let g_temp = "<<<<1<<ABC<<<<2<<TESTING<<<<3<<10/MAY/2016<<<<1<<ZXY<<<<2<<LOOK AT THIS<<<<3<<31/AUG/2016<<<<1<<REG<<<<2<<NICE ONE<<<<3<<04/NOV/2016"
  12.  
  13. LET idx = 0
  14. WHILE TRUE
  15.   LET pos1a = g_temp.getIndexOf("<<<<1<<",1)
  16.   LET pos2 = g_temp.getIndexOf("<<<<2<<",pos1)
  17.   LET pos3 = g_temp.getIndexOf("<<<<3<<",pos2)
  18.   LET pos1b = g_temp.getIndexOf("<<<<1<<",pos3)
  19.   IF pos1b = 0 THEN -- End of file
  20.      LET pos1b = g_temp.getLength()+1
  21.   END IF
  22.  
  23.   LET idx = idx + 1
  24.   LET g_arr[idx].r_code = g_temp.subString(pos1a+7,pos2-1)
  25.   LET g_arr[idx].r_desc = g_temp.subString(pos2+7,pos3-1)
  26.   LET g_arr[idx].r_date = g_temp.subString(pos3+7,pos1b-1)
  27.   LET g_temp = g_temp.subString(pos1b, g_temp.getLength())
  28.  
  29.   IF g_temp.getLength() = 0 THEN
  30.      EXIT WHILE
  31.   END IF
  32. END WHILE
  33. End Main

... you will need to add some additional code to handle cases where pos1,pos2,pos3 are zero, (and you should double check my maths) but the important thing is for you to see the base.String methods getIndexOf and subString.

Reuben

Product Consultant (Asia Pacific)
Developer Relations Manager (Worldwide)
Author of https://4js.com/ask-reuben
Contributor to https://github.com/FourjsGenero
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines