how can I count “CREATE GPL” in this case?

My DB generates 3000 lines for each process and I must count some values for a report. the generated file is like this :

CREATE TTL:NAME=SMO:0/TTS:0/UPL:1,BLQ=TRUE,NAND:TRUE,EBSPWRC=ADAPTIVE,EMSPWRC=ADAPTIVE
CREATE GPL,ASSLAPD=TSM:37/LPDLM:0,
CREATE GPL,ASSLAPD=TSM:38/LPDLM:5,
CREATE GPL,ASSLAPD=TSM:41/LPDLM:1,
CREATE GPL,ASSLAPD=TSM:21/LPDLM:8,
CREATE TTL:NAME=SMO:0/TTS:0/UPL:1,BLQ=FALSE,NAND:FALSE,EBSPWRC=ADAPTIVE,EMSPWRC=ADAPTIVE
CREATE GPL,ASSLAPD=TSM:37/LPDLM:4,
CREATE GPL,ASSLAPD=TSM:21/LPDLM:1,
CREATE TTL:NAME=SMO:0/TTS:0/UPL:1,BLQ=TRUE,NAND:TRUE,EBSPWRC=ADAPTIVE,EMSPWRC=ADAPTIVE
CREATE GPL,ASSLAPD=TSM:38/LPDLM:1,
CREATE GPL,ASSLAPD=TSM:41/LPDLM:1,
CREATE GPL,ASSLAPD=TSM:21/LPDLM:7,

Actually I want to count CREATE GPL if the BLQ and NAND be true, Also I wrote this method that read the file line by line :

public void getGPLCount(File f) throws Exception { 
    BufferedReader br = new BufferedReader(new FileReader(f));
    String line;
    while ((line = br.readLine()) != null) {
        if (line.startsWith("CREATE TTL:NAME=") && line.contains("BLQ=TRUE") && line.contains("NAND:TRUE"))
        //___//
    }
}

thnaks for your help ...

Answers


It appears as if Thomas's answer is close, but misunderstood what you wanted. I'm just modifying his snippet.

Something like this:

int counter = 0;
boolean countLines = false;
while ((line = br.readLine()) != null) {
  if (line.startsWith("CREATE TTL:NAME=") && line.contains("BLQ=TRUE") && line.contains("NAND:TRUE")) {
    countLines = true;
  } else if (line.startsWith("CREATE TTL:NAME=") && !(line.contains("BLQ=TRUE") || line.contains("NAND:TRUE"))){
    countLines = false;
  } else if( line.startsWith("CREATE GPL") && countLines )  {
    counter++;
  }
}

So start counting every time we get BOTH BLQ=true AND Nand=true, and stop the counter if we get a TTL line missing one (or both) of those. Is this the logic you want?

UPDATE:

Matthew's solution is also correct, but in pseudocode. Translating to Java:

int counter = 0;
boolean countLines = false;
while ((line = br.readLine()) != null) {
  // test for TTL line
  if (line.startsWith("CREATE TTL:NAME=")) { 
    countLines = (line.contains("BLQ=TRUE") && line.contains("NAND:TRUE"))
  // if TTL told us to count and it's a GPL line
  } else if(countLines && line.startsWith("CREATE GPL"))  {
    counter++;
  }
}

His answer is cleaner because he doesn't duplicate the startsWith("CREATE TTL:NAME=") like I did. In my defense, I was trying to get you a working java example and Thomas was extremely close to what you wanted.


Well, you might set some flag to true when you encounter a line with BLQ and NAND, set it to false when you encounter a line without BLQ, NAND or CREATE GPL and count if the line starts with CREATE GPL and the flag is true.

Something like this:

int counter = 0;
boolean countLines = false;
while ((line = br.readLine()) != null) {
  if (line.startsWith("CREATE TTL:NAME=") && line.contains("BLQ=TRUE") && line.contains("NAND:TRUE")) {
    countLines = true;
  } else if( line.startsWith("CREATE GPL") && countLines )  {
    counter++;
  } else {
    countLines = false;
  }
}

In pseudocode:

public void getGPLCount(File f) throws Exception { 
    for each line {
        if (line is a TTL line) {
            countIt = (if BLQ && NAND);
        } else {  
            if (countIt && is GPL line) {
                // we count it, increment count
            }
        }
    }

    System.out.println("count=" + count);
}

I guess that you need to actually count each block after the condition is me separately.

You're almost there:

public List<Integer> getGPLCount(File f) throws Exception { 
    BufferedReader br = new BufferedReader(new FileReader(f));
    String line;
    List<Integer> result = new ArrayList<Integer>();
    int count = 0;

    boolean counting = false; // are we counting in this iteration
    while ((line = br.readLine()) != null) {
        if (!line.startsWith("CREATE GPL")) { //BLOCK DELIMITER
            if (counting) { //WE WERE COUNTING UNTIL NOW, SO WE'RE ADDING THE COUNTER TO THE RESULT LIST AND STOP COUNTING 
                result.add(count);
                count = 0;
                counting = false;
            }
            if (line.startsWith("CREATE TTL:NAME=") && line.contains("BLQ=TRUE") && line.contains("NAND:TRUE")) {//WE START COUNTING

                counting = true;
            } 
        } else { // WE'RE INSIDE THE BLOCK, SO IF WE'RE COUNTING, WE COUNT
            if (counting) {
                ++count;
            }
        }

    }
if (counting) { //TAKE CARE OF WHAT'S LEFT
    result.add(count);

}

    br.close();
    return result;
} 

If you need to count all lines, just remove the result list and all references and don't reinitialize the counter.


Need Your Help

Submit form information multiple times

javascript jquery html forms coldfusion

I have a form with a multiple select option in it. I wanted to make it so that when the user hits submit, it will populate my DB with all the relevant fields, then go through some sort of loop for ...

Modal View prevents other views from rotating

iphone objective-c ios

I have this method in my MainViewController:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.