Compare certain lines in text files - php

Im trying to compare the first 10 lines of 100 lines of $completeGoogle(5000Lines) and count the number of matches with another file. However my count should be between 1-10 and I am getting an answer of 5010???
foreach(new SplFileObject($completeGoogle) as $n => $line)
if($n % 100 < 10)
{
$f_Api = fopen($apiFile,'r');
for ($i = 0 ;$i < 10; $i++)
{
$top10 = fgets($f_Api);
if ($line === $top10);
{
$count++;
}
}
fclose($f_Api);
}

You compare each 1-9, 100-109... line in one file with 10 lines from another and sum all matches in one variable $count.
If everything is equal you must get 500 (lines from first file) * 10 (lines from other) = 5000 matches.

The reason why this was failing my was because of a simple ";" after the if statement.
foreach(new SplFileObject($completeGoogle) as $n => $line)
if($n % 100 < 10)
{
$f_Api = fopen($apiFile,'r');
for ($i = 0 ;$i < 10; $i++)
{
$top10 = fgets($f_Api);
if ($line === $top10)
{
$count++;
}
}
fclose($f_Api);
}

Related

Can someone help me return a value?

I have created a loop which returns a random number between two values. Cool.
But now I want the script to return the following value too: The number of unique numbers between two similar numbers.
Example:
4
5
8
22
45
3
85
44
4
55
15
23
As you see there is a double which is the four and there are 7 numbers inbetween. So I would like the script to echo these numbers two so in this case it should echo 7 but if there are more doubles in the list it should echo all the numbers between certain doubles.
This is what I have:
for ($x = 0; $x <= 100; $x++) {
$min=0;
$max=50;
echo rand($min,$max);
echo "<br>";
}
Can someone help me or guide me? I'm learning :)
Thanks!
So You need to seperate script for three parts:
getting randoms and save them to array (name it 'result'),
analyze them,
print (echo) results
Simply - instead of printing every step of loop, save them to array(), exit loop, analyze every item with other, example:
take i element of list
check is i+j element is the same
if is it the same - save j-i to second array() (name it 'ranges')
And after this, print two arrays (named by me as 'result' and 'ranges')
UPDATE:
Here's solution, hope You enjoy:
$result = array(); #variable is set as array object
$ranges = array(); #same
# 1st part - collecting random numbers
for ($x = 0; $x < 20; $x++)
{
$min=0;
$max=50;
$result[] = rand($min,$max); #here's putting random number to array
}
$result_size = count($result); #variable which is containg size of $result array
# 2nd part - getting ranges between values
for ($i = 0; $i < $result_size; $i++)
{
for ($j = 0; $j < $result_size; $j++)
{
if($i == $j) continue; # we don't want to compare numbers with itself,so miss it and continue
else if($result[$i] == $result[$j])
{
$range = $i - $j; # get range beetwen numbers
if($range > 0 ) # this is for miss double results like 14 and -14 for same comparing
{
$ranges[$result[$i]] = $range;
}
}
}
}
#3rd part - priting results
echo("RANDOM NUMBERS:<br>");
foreach($result as $number)
{
echo ("$number ");
}
echo("<br><br>RANGES BETWEEN SAME VALUES:<br>");
foreach($ranges as $number => $range)
{
echo ("For numbers: $number range is: $range<br>");
}
Here's sample of echo ($x is set as 20):
RANDOM NUMBERS:
6 40 6 29 43 32 17 44 48 21 40 2 33 47 42 3 22 26 39 46
RANGES BETWEEN SAME VALUES:
For numbers: 6 range is: 2
For numbers: 40 range is: 9
Here is your fish:
Put the rand into an array $list = array(); and $list[] = rand($min,$max); then process the array with two for loops.
$min=0;
$max=50;
$list = array();
for ($x = 0; $x <= 100; ++$x) {
$list[] = rand($min,$max);
}
print "<pre>";print_r($list);print "</pre>";
$ranges = array();
$count = count($list);
for ($i = 0; $i < $count; ++$i) {
$a = $list[$i];
for ($j = $i+1; $j < $count; ++$j) {
$b = $list[$j];
if($a == $b) {
$ranges[] = $j-$i;
}
}
}
print "<pre>";print_r($ranges);print "</pre>";

Getting lines out of a long text

Ok, I got a digital book of 30.000 lines. I want to show only the first 20 lines of a chapter as a preview, every chapter got 300 lines.
Anyone an idea how to solve this? I tried the following:
foreach ($lines as $n => $line) {
if ($n >= 0 && $n =< 20) {
echo $line;
}
This will result in showing the first 20 lines of the first chapter. So how do I repeat this for all the other lines?
show 0-20
show 300-320
show 600-620
show 900-920
etc..
Thanks in advance!
You can do it with:
$rgChapters = [];
$rFile = fopen('/path/to/file', 'r');
$iLines = 20;
$iChapter = 300;
$i = 0;
while($sData = fgets($rFile) && !feof($rFile))
{
if($i % $iChapter < $iLines)
{
$rgChapters[floor($i/$iChapter)].=$sData.PHP_EOL;
}
$i++;
}
fclose($rFile);
-as a result, you'll get an array with first 20 lines every 300 lines (or you can directly output data rather than store it in array)
To print chapter 5
$chapter = 5;
$pages = 20;
$start = $chapter*$pages;
for($i=$start, $c=$start+$pages-1; $i < $c; $i++)
{
echo $lines[$i];
}
Just found the answer myself on an earlier SO question:
foreach ($lines as $n => $line) {
if ($n % 300 > 0 && $n % 100 <= 20) {
echo $line; // or whatever
}
}
Based on the answer written by Michael Berkowski (PHP read in every 10 out of 100 lines)

Modulus inside loop

I have an array that I am looping through and breaking up into chunks of 50. However occasionally the number of items inside that array are more than what fits inside that chunk of 50 ex.:
$array = array(); // has 220 rows
for ($i = 0; $i < count($array); $i++) {
$j[] = $i;
if ($i % 50 == 1) {
print_r($j); // do something here with the 50 rows
$j = null;
}
}
The problem here is that this will not print anything after 201. I know there is some algebraic math involved in solving this but I am drawing a blank. Its times like these where I really wish I had paid attention in math class back in high school.
I think array_chunk fits up your requirement and no maths required.
$result_array = array_chunk($array, 50, true);
Add additional condition
if ($i % 50 == 1 || count($array)-1 == $i)
You just have to redeclare the array is my guess:
$array = array(); // has 220 rows
for ($i = 0; $i < count($array); $i++) {
$j[] = $i;
if ($i % 50 == 1) {
print_r($j); // do something here with the 50 rows
$j = array() ;
}
}
Once you perform $j = null there is no way you can do $j[] = $i
$array = array(); // has 220 rows
for ($i = 0; $i < count($array); $i++) {
$j[] = $i;
if ($i % 50 == 1) {
doSomething($j); // do something here with the 50 rows
$j = array(); // reset the array
}
}
doSomething($j); // with the last 20 entries
After your loop is finished, you will have the remaining 201 through 220 entries in $j, so just do your stuff again.
array_chunk
might be useful. Basically splits the array into chunks returning a multi dimensional array

PHP Loop do action for each 10, 20, 30 etc [duplicate]

This question already has answers here:
PHP: How do you determine every Nth iteration of a loop?
(8 answers)
Closed 3 years ago.
In php i have loop e.g.
for ($i = 0; $i <= 1000; $i++) {
if ($i = 10 || $i == 20 || $i == 30 || $i == 40 || $i == 50 || $i == 60) {
echo $i;
}
}
imagine i need to echo $i every 10,20,30,40,50,60,..970,980,990 there should be way to not write 100 conditions in if statement. Is there some logical way to see if $i increased by 10 then do something like:
if ($i == $i+10) {
...
}
P.S. if possible i dont want to introduce another variable to count i need to find solution with using only $i
Try:
if ($i % 10 == 0)
That will trigger whenever your index is exactly divisible by 10.
rewrite your for loop to:
for ($i = 0; $i <= 1000; $i+=10) {
And I don't know whether it worked for you with commas like this (as in your initial post):
for ($i = 0, $i <= 1000, $i++) {
Skip extra looping:
for ($i = 10; $i <= 1000; $i=$i+10) {
echo $i;
}
Or if you still want to loop every single digit between:
for ($i = 0; $i <= 1000; $i++) {
if( $i % 10 === 0 ) {
echo $i;
}
}
Test Here
Put this inside your main loop. '%', or 'mod', gives you the remainder of $i / 10. If the remainder is '0', then you want to print.
if(0 === ($i % 10)) {
echo $i;
}

Step inside foreach

How to do something every 5 (for example) cycles inside foreach?
I'm add $i++ How to check it by step?
Use modulo to determine offset.
$i = 0;
foreach ($array as $a) {
$i++;
if ($i % 5 == 0) {
// your code for every 5th item
}
// your inside loop code
}
Unless you're doing something separately in each iteration, don't.
Use a for loop and increment the counter by 5 each time:
$collectionLength = count($collection);
for($i = 0; $i < $collectionLength; i+=5)
{
// Do something
}
Otherwise, you can use the modulo operator to determine if you're on one of the fifth iterations:
if(($i + 1) % 5 == 0) // assuming i starts at 0
{
// Do something special this time
}
for($i = 0; $i < $items; $i++){
//for every 5th item, assuming i starts at 0 (skip)
if($i % 5 == 0 && $i != 0){
//execute your code
}
}

Categories