Announcement

Collapse
No announcement yet.

Transforming repeating field

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transforming repeating field

    I have a SCH.8.1 that has repeating fields, so i'll have something along these lines.


    Code:
    SCH|PPP|1805||||EDIT||ABC~XYZ|45|min|202004070800||||||||||||||EDIT|
    Code:
    SCH|PPP|1427||||BOOK||ABC~XXX~XYZ|75|min|202004071300||||||||||||||BOOK|
    I need to transform ABC~XYZ into ABC-XYZ or ABC~XXX~XYZ into ABC-XXX-XYZ, basically converting the repeating fields or replacing the tilde with a -. Can't wrap my head around it at the moment.

    So when the length of msg['SCH'[['SCH.8']['SCH.8.1'] is greater than 1, I need to build that out backwards so the order of the text stays the same and add a '-' to the middle making sure i dont add one after the last index, so that it doesn't end up looking like ABC-XYZ- or ABC-XXX-XYZ-
    Last edited by kab7715; 04-08-2020, 04:27 PM.

  • #2
    So I have some progress and the string is formatted correctly using a suggestion for a different thread, having an issue re-assigning to the SCH.8.1 field

    Code:
    var sch81 = msg['SCH']['SCH.8']['SCH.8.1'];
    
    //uses java stringBuilder object
    var formattedSch81 = new java.lang.StringBuilder();
    
    //checks to see if there more than 1 repating field
    if(sch81.length() > 1)
    {
    	for each(sch in sch81)
    	{
    		//appends each repeating field in correct order and adds a '-' to the end
    		formattedSch81.append(sch.toString()).append('-');
    	}
    	//this removes the last '-' that that sting builder appended to the last value
    	formattedSch81 = formattedSch81.substring(0, formattedSch81.length()-1);
    	//delete the msg.SCH.8 if length is more than 1
    	delete msg['SCH']['SCH.8'];
    }
    
    msg['SCH']['SCH.8'] = formattedSch81.toString();
    Having an issue assigning the formattedSch81 variable to the now empty SCH.8.1, any thoughts?
    Last edited by kab7715; 04-08-2020, 06:05 PM.

    Comment


    • #3
      Figured it out, might be a leaner way of doing it but its working!

      Code:
      var sch81 = msg['SCH']['SCH.8']['SCH.8.1'];
      
      //uses java stringBuilder object
      var formattedSch81 = new java.lang.StringBuilder();
      
      //checks to see if there more than 1 repating field
      if(sch81.length() > 1)
      {
      	for each(sch in sch81)
      	{
      		//appends each repeating field in correct order and adds a '-' to the end
      		formattedSch81.append(sch.toString()).append('-');
      	}
      	//this removes the last '-' that that sting builder appended to the last value
      	formattedSch81 = formattedSch81.substring(0, formattedSch81.length()-1);
      	
      	//delete the msg.SCH.8 if length is more than 1, need to clear all of SCH.8 and not just SCH.8.1 because the tildes will stay behind
      	msg['SCH']['SCH.8'] = '';
      }
      
      //assigning the formatted new sch 8.1 to field
      msg['SCH']['SCH.8']['SCH.8.1'] = formattedSch81.toString();

      Comment


      • #4
        Maybe leaner:

        Code:
        var sch8 = ''
        for (var i=0;i<msg.SCH['SCH.8'].length(); i++) {
        	sch8 = sch8 + msg.SCH['SCH.8'][i]['SCH.8.1'].toString() + '-'
        }
        msg.SCH['SCH.8'] = ''
        msg.SCH['SCH.8']['SCH.8.1'] = sch8.slice(0,-1)

        Comment


        • #5
          ...including the if statement to check for repeats.

          Code:
          if (msg.SCH['SCH.8'].length() > 1) {
          	var sch8 = ''
          	for (var i=0;i<msg.SCH['SCH.8'].length();i++) {
          		sch8 = sch8 + msg.SCH['SCH.8'][i]['SCH.8.1'].toString() + '-'
          	}
          	msg.SCH['SCH.8'] = ''
          	msg.SCH['SCH.8']['SCH.8.1'] = sch8.slice(0,-1)
          }
          Last edited by jkrebs; 04-09-2020, 01:12 PM.

          Comment


          • #6
            As we can see, there are many ways to do this. Here's how I would do it.

            PHP Code:
            var sch81 = [];
            for 
            each (var rep in msg.SCH['SCH.8']['SCH.8.1']) {
                
            sch81.push(rep.toString());
            }
            msg.SCH['SCH.8'] = <SCH.8><SCH.8.1>{sch81.join('-')}</SCH.8.1></SCH.8>; 
            There's no need to check if the length is greater than 1 because the loop works whether there are 0, 1, or more repetitions.

            Rather than deleting or setting an intermediate empty string value, I'm directly building the xml that I want to replace the old xml.
            Last edited by agermano; 04-09-2020, 06:39 PM.

            Comment


            • #7
              I like the lean XML direct-build option as well...
              Last edited by jkrebs; 04-09-2020, 07:53 PM.

              Comment

              Working...
              X