Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik GA: Multivariate Time Series in Qlik Predict: Get Details
cancel
Showing results for 
Search instead for 
Did you mean: 
kakooo16
Creator
Creator

Store GlobalVariable in String array

Hello , 

 

Im trying to store a list of 'marketplace_product_id' but im always , but im getting one row  (which is not the case )

 

0683p000009M9Su.png

 

Thanks .

Labels (2)
1 Solution

Accepted Solutions
billimmer
Creator III
Creator III

Make sure you set your tloop type as a "While' loop.  Not a for loop.

View solution in original post

8 Replies
kakooo16
Creator
Creator
Author

Hello again , 

 

Could i declare an array list using a globalvariable ? . 

 

Im still blocked please !

billimmer
Creator III
Creator III

Here is some working code from one of my jobs.  It's in a tJavaFlex which creates an ArrayList and stores it in a global variable.  What the code does is break a large file into blocks based on begin/end tags.  It stores a list of the blocks in the ArrayList for use later in the job.  You can use this as an example:

 

Start Code:

int i = 0;
String s;

boolean myStart = false;
boolean myRecCntFlag = false;

int startPos;
int endPos;
String subS;

List<String> listFilenames = new java.util.ArrayList<String>();
List<Boolean> listmyRecCntFlags = new java.util.ArrayList<Boolean>();

globalMap.put("myFilename", "nothing");
globalMap.put("myRecCntFlag", false);

 

Main Code:

s = row7.line;

if(!myStart) {
if(s.contains("^^^BREAK-ETL-BEGIN")) {
myStart = true;

// IndexOf returns -1 if there nothing is found
startPos = s.indexOf(":");
endPos = s.indexOf(":", startPos + 1);
//System.out.println(startPos + "-" + endPos);
if(startPos>=0 && endPos>=0 && startPos < endPos) {
subS = s.substring(startPos + 1, endPos);
} else {
subS = Integer.toString(i);
}

subS = subS + ".sql";

listFilenames.add(subS);
globalMap.put("myFilename", subS);
s = "";
i++;
}
} else {
if(s.contains("^^^BREAK-ETL-END^^^")) {
listmyRecCntFlags.add(myRecCntFlag);
globalMap.put("myRecCntFlag", myRecCntFlag);

myStart = false;
myRecCntFlag = false;
s = "";
} else {
if(s.contains("RecCnt")) {
myRecCntFlag = true;
}
}
}

row9.line = s;

 

End Code:

globalMap.put("myManafestFilenames", listFilenames);
globalMap.put("myManafestRecCntFlags", listmyRecCntFlags);

//System.out.println("test: " + (String)globalMap.get("myFilename"));

 

 

Then a tloop is used to iterate through the ArrayList like this:

 

Declaration: int i=0

Conndition: i<((List)globalMap.get("myManafestFilenames")).size()

Iteration: i++

 

Your value can then be accessed with:

 

(String)((List)globalMap.get("myManafestFilenames")).get(((Integer)globalMap.get("tLoop_1_CURRENT_ITERATION")) - 1)

Anonymous
Not applicable

You always get the last item with this job design, If you want to add each item to string array, to do:
1. Declare an arraylist in the begin part of tJavaFlex.
2. Add each item to the list in the main part of tJavaFlex.
3. In the end part of tJavaFlex, convert the list to string array if needed.

Regards
Shong
kakooo16
Creator
Creator
Author

The problem is when i print the value of the global varible as such : 

 

System.out.println("Global variable : " + globalMap.get("marketplace_product_id"));

 

The result is  : Global variable : null

 

Am i missing something while putting my variable into tSetGlobalVar ? 

 

Thanks
  

Anonymous
Not applicable

First, make sure you read data out from memory using tHashInput.
Can you upload a screenshot of basic setting of tSetGlobalVar?
kakooo16
Creator
Creator
Author

Here is a screenshot

0683p000009M9Tn.png

kakooo16
Creator
Creator
Author

When i try to declare int i=0 in the tloop component . It is getting me an error int cannot be resolved .
billimmer
Creator III
Creator III

Make sure you set your tloop type as a "While' loop.  Not a for loop.