Automic Workload Automation

  • 1.  Splitting a string using a delimiter with unknown number of columns

    Posted May 15, 2015 11:28 AM
    I'm using a text field in a prompt set to have users specify multiple files separating each one by a comma. I need to then separate these file names from the variable. However, there could be 2 files specified or 20. I have a solution for this that I've used before essentially looking for the delimiter, taking the difference between the starting point and the delimiter and cutting that out and setting to a variable but I'm looking for a better way, if possible
      
    Basically I need this..

    &STRING# = 'JIM1.txt, JIM2.txt, JIM3.txt'

    to be 

    &STRING1# = 'JIM1.txt'
    &STRING2# = 'JIM2.txt'
    &STRING3# = 'JIM3.txt'

    With the STR_SPLIT, it looks like I setup an array but I have to specify  the size of the array, which is unknown. I would have to script it to discover the number of commas, then set the array to that size and do the split. 

    So...does anyone have an easier way to do this?

    Thanks for any and all help!

    Jim


  • 2.  Splitting a string using a delimiter with unknown number of columns

    Posted May 15, 2015 11:47 AM
    Perhaps the How to process an arbitrarily-sized list of items discussion will be of assistance.

    I have also thought about creating a "temporary" Variable object that I place each delimited string into and then use a PREP_PROCESS_VAR to get the individual values from as needed.  But since my existing common Include works reliably I've not had the need nor inclination.


  • 3.  Splitting a string using a delimiter with unknown number of columns

    Posted May 15, 2015 11:56 AM
    Wow that's crazy ugly for what's trying to be done. It seems like since 5.1, which would have come out in what 2005/2006?, that a better solution could have been created using a function. Even the ability to search a string for the number of times a specific character or string of characters appears would be beneficial. 


  • 4.  Splitting a string using a delimiter with unknown number of columns

    Posted May 15, 2015 12:12 PM
    Well I'm a bit more clever now than I was then but I'm still crazy.  >:)    

    Since this logic is "hidden" away in an Include it doesn't look too ugly in the scripts that use it.  Its ugliness rears its head however when debugging and using the Activation protocol trace for those scripts.


  • 5.  Splitting a string using a delimiter with unknown number of columns

    Posted May 15, 2015 12:14 PM
    Haha...yes I also use a few includes to hide some ugliness. Here's my solution. 

    :SET &STRING# = 'JIM1.TXT, JIM2.TXT, JIM3.TXT, JIM4.TXT'
    :SET &MULT# = STR_FIND(&STRING#, ',')
    :SET &MULT# = FORMAT(&MULT#)
    !
    :IF &MULT# > '0'
    :  SET &BEG# = '1'
    :  SET &COMMA# = 'Y'
    :  WHILE &COMMA# = 'Y'
    :    SET &END# = STR_FIND(&STRING#,',',&BEG#)
    :    IF &END# > '0'
    :      SET &COMMA# = 'Y'
    :       SET &LGTH# = SUB(&END#,&BEG#)
    :       SET &FILE# = STR_CUT(&STRING#,&BEG#,&LGTH#)
    :       SET &FILE# = STR_TRIM(&FILE#)
    :       PUT_VAR VARA.STRING_SPLIT, &FILE#,
    :       PRINT "&FILE#"
    :      ELSE
    :        SET &COMMA# = 'N'
    :        SET &FILE# = STR_CUT(&STRING#,&BEG#)
    :        SET &FILE# = STR_TRIM(&FILE#)
    :        PUT_VAR VARA.STRING_SPLIT, &FILE#,
    :        PRINT "&FILE#"
    :    ENDIF
    :    PRINT "COMMA FOUND? &COMMA#"
    :    SET &FILE# = STR_TRIM(&FILE#)
    :    SET &BEG# = ADD(&END#,1)
    :    SET &BEG# = FORMAT(&BEG#)
    :    PRINT &BEG#
    :  ENDWHILE
    :ELSE
    :    SET &FILE# = STR_TRIM(&STRING#)
    :    PUT_VAR VARA.STRING_SPLIT, &FILE#,
    :    PRINT "FILE#"
    :ENDIF


  • 6.  Splitting a string using a delimiter with unknown number of columns

    Posted May 15, 2015 07:50 PM
    Here is my favorite way of doing it;
    :SET &STRING# = 'JIM1.TXT, JIM2.TXT, JIM3.TXT, JIM4.TXT' :set &Delm# = "," ! :delete_var VARA.STRING_SPLIT :SET &MYSTRING# = &STRING# :SET &COMMA# = str_find(&MYSTRING#,&Delm#) :WHILE &COMMA# > 0 :  set &OneItem# = str_split(&MYSTRING#,&Delm#) :  print "One item: '&OneItem#'" :  PUT_VAR VARA.STRING_SPLIT, &OneItem#, :  set &COMMA# = &COMMA# + 1 :  set &MYSTRING# = substr(&MYSTRING#,&COMMA#) :  set &MYSTRING# = str_ltrim(&MYSTRING#) :  SET &COMMA# = str_find(&MYSTRING#,&Delm#) :ENDWHILE :print "last item: '&MYSTRING#'" :PUT_VAR VARA.STRING_SPLIT, &MyString#,

    Most of these solutions are susceptible to failure when there is a comma on the end of the string, two commas together in the middle, etc.  If it can happen, then you should code for it.