Jump to content

[SOLVED] Remove dublicated


raimis100

Recommended Posts

Hey!

 

I am thinking of way to remove duplicate lines in my txt file

each sentence is in the different line

 

So far I did is

 


<?php

  $list = file ('sentencelist.txt');
  For ($I = 0; $I <= count ($list) - 1; $I++)
  {
    $dub = false;
    $corrent = trim ($list [$I]);
    For ($X = $I + 1; $X <= count ($list) - 1; $X++)
    {
      If ($corrent == trim ($list [$X]))
      {
        $dub = true;
        break;
      }
    If ($dub == False) 
    {
      $output = $output . $corrent ."\n";
    }
    }
  }
  echo $output;
  
?>

 

basically what it does is compare each line but it crashed my apache because my txt files are huge [ around 100 k lines]

 

Can anybody suggest me better way to do it

 

 

edit:

 

 

actually I found a bug. Script should be

 


<?php

  $list = file ('sentencelist.txt');
  For ($I = 0; $I <= count ($list) - 1; $I++)
  {
    $dub = false;
    $corrent = trim ($list [$I]);
    For ($X = $I + 1; $X <= count ($list) - 1; $X++)
    {
      If ($corrent == trim ($list [$X]))
      {
        $dub = true;
        break;
      }
    }
    If ($dub == False) 
    {
      $output = $output . $corrent ."\n";
    }
  }
  echo $output;
  
?>

 

But there is still high cpu usage.

Link to comment
https://forums.phpfreaks.com/topic/117202-solved-remove-dublicated/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.