• Support
  • Articles
  • Resources
  • Products

Create Array from String with > 9 'Elements'

Moderator: boris

4 postsPage 1 of 1

stevo
Newbie

Posts:
19
Joined: Tue Oct 15, 2013 11:08 pm
by stevo » Mon Aug 25, 2014 2:06 am
Using this loop you can take a character separated string of elements up to the maximum Array Element Count and recursively break it into array elements:

Code: Select all

  1.  
  2. closett ; not needed for this demo
  3.  
  4. :InitialValues
  5. ; create an arbitrary string with greater than 9 'character-separated-elements' (you must choose the separation character; e.g. a comma ",")
  6. sprintf2 csvStr '1-d,2-sdifu,3-werwerwe,4-sut,5-a43234,6-ertrert345,7-,8-e,9-lk3f4f,10-bash,11-we534es,12-wierjd,13-w409excje3oid,14-xci,15-2398,16-ck4oici9,17-2398fuse,18,19,20,21,22,23,24,25,26,,,,f,'
  7. ; alternative: use InputBox method
  8. /*
  9. InputBox 'Enter a separated string to process' 'Get Input String from User'
  10. csvStr = inputstr
  11. */
  12. ArrSize = 0
  13. ArrBase = 0
  14.  
  15. :loop
  16. messagebox csvStr 'csvStr ='
  17. StrSplit csvStr ','
  18. StrCount = result
  19. Str1 = groupmatchstr1
  20. Str2 = groupmatchstr2
  21. Str3 = groupmatchstr3
  22. Str4 = groupmatchstr4
  23. Str5 = groupmatchstr5
  24. Str6 = groupmatchstr6
  25. Str7 = groupmatchstr7
  26. Str8 = groupmatchstr8
  27. Str9 = groupmatchstr9
  28.  
  29. ;messagebox StrCount 'how many elements?'
  30. if StrCount > 9 then
  31.     Count = 9
  32. else
  33.     Count = StrCount
  34. endif
  35.  
  36. For x 1 Count
  37.     y = (x + ArrBase)
  38.     sprintf2 cmd 'var%d = Str%d' y x
  39.     execcmnd cmd
  40.     ;sprintf2 cmd 'messagebox var%d "var%d ="' y y
  41.     ;execcmnd cmd
  42.     sprintf2 cmd "StrReplace csvStr 1 var%d ''" y
  43.     execcmnd cmd
  44.     StrTrim csvStr ','
  45.     ArrSize = ArrSize + 1
  46. Next
  47. ArrBase = ArrSize
  48. ;messagebox ArrBase 'ArrBase'
  49. ;messagebox csvStr 'csvStr is now:'
  50.  
  51. if StrCount > 9 goto loop
  52.  
  53. ;messagebox ArrSize 'ArrSize'
  54. StrDim StrArr ArrSize
  55.  
  56. For x 1 ArrSize
  57.     sprintf2 cmd 'StrArr[%d] = var%d' x-1 x
  58.     ;messagebox cmd 'cmd = '
  59.     execcmnd cmd
  60. Next
  61.  
  62. ListBox 'The resultant array' 'String Array' StrArr


It uses the StrSplit (string split) function to read the elements into temporary numbered variables 8 at a time until there are none left (strsplit works on up to 9 elements - you specify the character to use as the separator and if there are more than 9 the rest are all added to groupmatchstr9).

Once all elements have been read into a numbered variable, an array is created for the total number found and they are copied to the array.

Please test this and let me know if you find any mistakes!
HTH :)
IshmaelCallMe
Guru

Posts:
672
Joined: Thu Jan 26, 2006 2:28 am
by IshmaelCallMe » Thu Nov 05, 2015 6:47 pm
Fixed a minor bug.

Here is another solution:

Code: Select all

  1.  
  2.      
  3.     fpga_v='s1 s2 s3 s4 s5 s6 s7 s8 s9 s10 s11 s12 s13'
  4.     n=0
  5.     do
  6.       strscan fpga_v ' '
  7.       if result=0 strlen fpga_v
  8.       if result<1 break
  9.       strcopy fpga_v 1 result tmp
  10.       strcopy fpga_v result+1 9999 fpga_v
  11.       n=n+1
  12.       sprintf2 tmp 'vr%i="%s"' n tmp
  13.       execcmnd tmp
  14.     loop
  15.     for i 1 n
  16.       sprintf2 tmp "messagebox vr%i 'vr%i'" i i
  17.       execcmnd tmp
  18.     next
  19.      
  20.  
IshmaelCallMe
Guru

Posts:
672
Joined: Thu Jan 26, 2006 2:28 am
by IshmaelCallMe » Thu Dec 03, 2015 5:40 pm
To use an actual array:

Code: Select all

  1.  
  2. strdim vr 200   ;expected max number of substrings
  3. fpga_v='s1 s2 s3 s4 s5 s6 s7 s8 s9 s10 s11 s12 s13'
  4. n=-1
  5. do
  6.   strscan fpga_v ' '
  7.   if result=0 strlen fpga_v
  8.   if result<1 break
  9.   strcopy fpga_v 1 result tmp
  10.   strcopy fpga_v result+1 9999 fpga_v
  11.   n=n+1
  12.   sprintf2 tmp 'vr[%i]="%s"' n tmp
  13.   execcmnd tmp
  14. loop
  15. ListBox 'The resultant array' 'String Array' vr
  16.  
IshmaelCallMe
Guru

Posts:
672
Joined: Thu Jan 26, 2006 2:28 am
by IshmaelCallMe » Thu Dec 03, 2015 6:39 pm
Finally, here is a general purpose subroutine that will correctly dimension the array, will not eat the original line, and supports single or multiple character separators:

Code: Select all

  1.  
  2. line='s1 s2 s3 s4 s5 s6 s7 s8 s9 s10 s11 s12 s13'
  3. subsep=' s'
  4. call s_split
  5. ListBox 'The resultant array' 'String Array' a_result
  6. messagebox n 'number in array'
  7. end
  8.  
  9. :s_split
  10. strlen subsep
  11. tmp2=result
  12. n=1
  13. tmp=line
  14. do
  15.   strscan tmp subsep
  16.   if result<1 break
  17.   strcopy tmp result+tmp2 9999 tmp
  18.   n=n+1
  19. loop
  20. strdim a_result n+1
  21. tmp=line
  22. for tmp3 0 n
  23.   strscan tmp subsep
  24.   if result=0 strlen tmp
  25.   strcopy tmp 1 result-1 tmp1
  26.   strcopy tmp result+tmp2 9999 tmp
  27.   sprintf2 tmp1 'a_result[%i]="%s"' tmp3 tmp1
  28.   execcmnd tmp1
  29. next
  30. return
  31.  
Display posts from previous:
Sort by:

4 postsPage 1 of 1

Users browsing this forum: No registered users
cron