cancel
Showing results forĀ 
Search instead forĀ 
Did you mean:Ā 
cancel
772
Views
0
Helpful
7
Replies

How to iterate a CSV String using Index

shabalas
Cisco Employee
Cisco Employee

Hi Team

I have a CSV String. I need to iterate through the string and use Index to address to a particular element in String. I have used Split String activity but it just runs in the For Loop to get the values.

I need to have the provision to take 'i' th element using an index which I can run in the loop.

In other words, could I have the provision of array definition in CPO and refer  any 'i' th position in the array using an index

Thanks                   

2 Accepted Solutions

Accepted Solutions

Hi, Shankar!

Using the XSL Transform activity in the product with the XSLT below you should be able to convert your comma-separated string into a XML that can be read into a table with 2 columns (Idx and Value, where Idx is the numeric number of the row).

The XML produced by the XSL Transform activity could be read into a table using Read Table from XML activity.

The XSL is expecting that you will put your string into tag.


xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 
   
         
           
           
         
   
 

 
   
   
   
     
       
         
         
       
       
         
         
       
     
     
       
         
         
       
     
   
 
 
 
   
   
   
     
       
         
       
       
         
       
     
   
 

Svetlana


					
				
			
			
				
			
			
				
			
			
				

View solution in original post

I'd go with regex for a string like that.

Prepend a comma to your string by hardcoding it so that your input looks like

,[Process.Variables.Local.CSVString]

Then use the following regex:

(?<=(.*?\,){[Process.Variables.Local.Nth]})\w*

The input of

,val1,val2,val3

to regex

(?<=(.*?\,){2})\w*

will return "val2" in "First Match" of the regex activity output properties.

View solution in original post

7 Replies 7

Shaun Roberts
Cisco Employee
Cisco Employee

I suppose you could write a function to take a string and convert it into a 2 column array where one column is the character (or string) and one column is the index. Then you can do select statements to pull the exact index.

You cannot call a string (or even array) like you would in native programming language since the for-each loop is not a true for loop, it's made for tables.

--Shaun Roberts
Principal Engineer, CX
shaurobe@cisco.com

This can't be done in a single step, but depending on what you have, it can probably be done in more than one step.

Is it a multi-line, multi-value string?  Most CSVs are of the form:

Value1,Value2,Value3,...

Value4,Value5,Value6,...

...

Are you trying to get to "Value5" above?

You might be able to use Regex, or the Read Table From Text followed by a Select From Table, or an XSL Transform to achieve what you want.

What does your string look like?

My String  is Single line and looks like:

val1,val2,val3

Hi, Shankar!

Using the XSL Transform activity in the product with the XSLT below you should be able to convert your comma-separated string into a XML that can be read into a table with 2 columns (Idx and Value, where Idx is the numeric number of the row).

The XML produced by the XSL Transform activity could be read into a table using Read Table from XML activity.

The XSL is expecting that you will put your string into tag.


xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 
   
         
           
           
         
   
 

 
   
   
   
     
       
         
         
       
       
         
         
       
     
     
       
         
         
       
     
   
 
 
 
   
   
   
     
       
         
       
       
         
       
     
   
 

Svetlana


					
				
			
			
				
			
			
				
			
			
			
			
			
			
		

Once you have the data in a table like this you can either iterate over all values in a while loop.

Or you can use Select from Table activity to select a row with a specific Idx value.

I'd go with regex for a string like that.

Prepend a comma to your string by hardcoding it so that your input looks like

,[Process.Variables.Local.CSVString]

Then use the following regex:

(?<=(.*?\,){[Process.Variables.Local.Nth]})\w*

The input of

,val1,val2,val3

to regex

(?<=(.*?\,){2})\w*

will return "val2" in "First Match" of the regex activity output properties.

Mike's regular expression support is much lighter implementation if all you want to retrieve is an nth entry in the list.