0

Continuing on my previous question.

I have text log file called reject. You can see it here

As you can see there are 4 steps on tab called:

1. Battery level
2. Piezo sound level
3. Left D3  (Ch 3) light intensity
4. Right D2  (Ch 1) light intensity

enter image description here

Now I want to count every row with condition:

Which column(Steps) value is filled then count it.
Example: on row 1, We can see the value 0 (any value) is on step Piezo sound level. Then count it.

So finally I can know how many quantity Reject Process.

Battery level = x quantity
Piezo sound level = x quantity
Left D3  (Ch 3) light intensity = x quantity
Right D2  (Ch 1) light intensity = x quantity

The PHP Code:

$fromDateTime = new DateTime('Wed, Sep 19  2018 08:00:00');
$toDateTime = new DateTime('Wed, Sep 19  2018 19:59:00');
$file = file_get_contents('reject.txt');
$lines = explode("\n",$file);

// counter
$rowsintimespan = 0;

// Do Line-By-Line starting by Line 16 (Array Index 15)
for($i = 15; $i < count($lines); $i++) {
// if the file is "Tue, Sep 18<tab>2018<tab>23:59:53<tab>"
$dateobj = DateTime::createFromFormat("???,?M?d??Y?H:i:s+", $lines[$i]);

// check if date is in your Timespan
if($dateobj < $toDateTime && $dateobj > $fromDateTime) {
        $rowsintimespan++; // count if in timespan
    }
}

// Debug-Output
echo $rowsintimespan;

UPDATE

I need to read the last column value, Example: if the value of row is on column Left D3 then count it. If the value of row is on column Piezo then count it.

enter image description here

2
  • So you only want to count the last value of a row? I.e. if batterylevel and piezo are set, only count piezo? If Battery, piezo and left are set, then just count left? Commented Sep 19, 2018 at 12:13
  • Yes... exactly Sir Commented Sep 19, 2018 at 12:14

2 Answers 2

1

if you're ok with writing down your columns as keys then this should work as you described:

$fromDateTime = new DateTime('Wed, Sep 19  2018 08:00:00');
$toDateTime = new DateTime('Wed, Sep 19  2018 19:59:00');
$file = file_get_contents('Reject.txt');
$lines = explode("\n", $file);

// counter
$rowsintimespan = 0;
// keys should correspond to columns
$keys = [
    'date',
    'time',
    'battery',
    'piezo',
    'leftD3',
    'rightD2'
];

$values = array_fill(0, count($keys), 0);
$values = array_combine($keys, $values);

// Do Line-By-Line starting by Line 16 (Array Index 15)
for ($i = 11; $i < count($lines); $i++) {
    // if the file is "Tue, Sep 18<tab>2018<tab>23:59:53<tab>"
    $dateobj = DateTime::createFromFormat("???,?M?d??Y?H:i:s+", $lines[$i]);

    // check if date is in your Timespan
    if ($dateobj < $toDateTime && $dateobj > $fromDateTime) {
        $rowsintimespan++; // count if in timespan

        // get line elements
        $lineContent = explode("\t", $lines[$i]);

        // loop through line elements and count them
        $x = 0;
        for ($j = 0; $j < count($keys); $j++) {
            if (!isset($lineContent[$j])) {
                continue;
            }

            // remember position of last not empty column
            if (trim($lineContent[$j]) != '') {
                $x = $j;
            }
        }

        if ($x > 0) {
            $values[$keys[$x]]++;
        }
    }
}

// Debug-Output
echo $rowsintimespan;

// Output every column
echo '<pre>';
print_r($values);

This will print out:

Array
(
    [date] => 0
    [time] => 0
    [battery] => 4
    [piezo] => 31
    [leftD3] => 17
    [rightD2] => 1
)
Sign up to request clarification or add additional context in comments.

13 Comments

Hi Alex, I got error Notice: Undefined index: date in C:\xampp\htdocs\test\test.php on line 39 Notice: Undefined index: time in C:\xampp\htdocs\test\test.php on line 39 Notice: Undefined index: battery in C:\xampp\htdocs\test\test.php on line 39 Notice: Undefined index: piezo in C:\xampp\htdocs\test\test.php on line 39 Notice: Undefined index: leftD3 in C:\xampp\htdocs\test\test.php on line 39 Notice: Undefined index: rightD2 in C:\xampp\htdocs\test\test.php on line 39
Sorry, Dave, I had error_reporting off, I edited the code to avoid notices
Hi Alex, I tried to count manually, the only 1 correct is Right D2 = 1, another is wrong. Here is my manually count to log file: Battery: 5 Right D2: 1 Left D3: 27 Piezo: 52
@HiDayurieDave I finally understood what you asked for. Try this updated code
Hi Alex, just now checked only Right D2 is correct. Another is wrong. Should be Battery: 5 Right D2: 1 Left D3: 26 Piezo: 52
|
0

Just split your row by your tabs and check for !empty();

//Set Time-Span
$fromDateTime = new DateTime('Wed, Sep 19  2018 00:00:00');
$toDateTime = new DateTime('Wed, Sep 19  2018 17:00:00');

// Load File
$file = file_get_contents('Reject.txt');

// Split by lines
$lines = explode("\n",$file);

// counter
$rowsintimespan = 0;
$rowswithbattery = 0;

// Do Line-By-Line starting by Line 16 (Array Index 15)
for($i = 15; $i < count($lines); $i++) {
    // if the file is "Tue,<space>Sep<space>18<space><space>2018<tab>23:59:53<tab>"
    $dateobj = DateTime::createFromFormat("???, M  d?Y?H:i:s+", $lines[$i]);

    // check if date is in your Timespan
    if($dateobj < $toDateTime && $dateobj > $fromDateTime) {
        $rowsintimespan++; // count if in timespan
        $cols = explode("\t",$lines[$i]);
        // 0 = Date, 1 = Time, 2 = Battery Level, 3 = Piezo, 4 = left, 5 = Right
        // Count Battery-Values
        if (!empty($cols[2])) {
            $rowswithbattery++;
        }
    }
}

// Debug-Output
echo 'In Timespan: '.$rowsintimespan."\n";
echo 'Rows With Battery: '.$rowswithbattery

Produces:

In Timespan: 84
Rows With Battery: 84

Full Example with all your columns: https://ideone.com/VAQH7h

Also you could iterate over every header and create a neat array as I showed in https://ideone.com/YUU1jP

// Split by lines
$lines = explode("\n", $file);
$header = explode("\t", $lines[9]);
// counter
$rowsintimespan = 0;
$counter = Array();

// Create an entry for every header and trim it to lose additional whitespaces
foreach($header as $index => $head){
    $counter[rtrim($head)] = 0;
}

// Do Line-By-Line starting by Line 16 (Array Index 15)
for($i = 11; $i < count($lines); $i++) {
    // the file is "Tue,<space>Sep<space>18<space><space>2018<tab>23:59:53<tab>"
    $dateobj = DateTime::createFromFormat("???, M d  Y?H:i:s+", $lines[$i]);
    // check if date is in your Timespan
    if($dateobj < $toDateTime && $dateobj > $fromDateTime) {
        $rowsintimespan++; // count if in timespan
        $cols = explode("\t",$lines[$i]);
        // 0 = Date, 1 = Time, 2 = Battery Level, 3 = Piezo, 4 = left, 5 = Right
        // Count Battery-Values
        foreach($header as $index => $head){
            // For every header check if col is empty
            $counter[rtrim($head)] += empty($cols[$index]) ? 0 : 1;
        }
    }
}

// Debug-Output
echo 'In Timespan: '.$rowsintimespan."\n";
var_dump($counter);

Update to match the updated question:

Then just do some simple math.

Theory: if you have 80 set columns with battery, and 20 with set piezo, you can substract the 20 piezo counter from your 80 battery counter and then you know how many last battery-columns you have and how many last piezo columns.

Thus:

$lastwithright = $rowswithright;
$lastwithleft = $rowswithleft - $rowswithright;
$lastwithpiezo = $rowswithpiezo - $rowswithleft;
$lastwithbattery = $rowswithbattery - $rowswithpiezo;

6 Comments

Hi Bam, I'm updated my question above, please have a look
Tried and got the result is wrong, refer to the log file that I share.lastwithright: 84 lastwithleft: -57 lastwithpiezo: 52 lastwithbattery: 5
I try to manually count, the correct value should be: ` `Battery: 5 Right D2: 1 Left D3: 27 Piezo: 52 ``
@Hidayurie Dave I don't think so, works perfectly: ideone.com/9HivaU I'm really wondering what you are doing. You have noticed that the snippet here is not complete, right? Habve you read, that the full code is on ideone? Because it works without modifcation.
Hi Bam, I just tried to use your code on ideone.com/9HivaU (I'm using your pure code) but still get the wrong value, In Timespan: 84 Rows With Battery: 84 Rows With Piezo: 79 Rows With Left: 27 Rows With Rigth: 83 Rows ending with Right:83 Rows ending with Left:-56 Rows ending with Piezo:52 Rows ending with Battery:5
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.