How to solve this problem by taking the first 1000 values of an array

Source: Internet
Author: User
Take the first 1000 values of an array & lt ;? Php $ fnvarloghaproxy. log.2; $ fpfopen ($ fn, r) ordie (& quot; fileopen $ fnfalse & quot;); while ($ sfgets ($ fp) {& nbsp; preg_match ([[d. :] +]. + [([obtain the first 1000 values of an array

$ Fn = '/var/log/haproxy. log.2 ';
$ Fp = fopen ($ fn, 'r') or die ("file open $ fn false ");
While ($ s = fgets ($ fp )){
Preg_match ('/\ [\ d.:] + \]. + \ [([\ d.:] +) \]/', $ s, $ r );
If (empty ($ r [1]) continue;
@ $ Res [$ r [1] ++;
}



Fclose ($ fp );
Asort ($ res );

Print_r ($ res );
?>

------ Solution --------------------
You cannot solve this problem.

Separate processing .....

For example, you can write the ip address in the first place to 255 files, or convert the ip address to a number. in this way, you can use less memory for subsequent processing.

Find the first 255 files... Because each group contains at most 2 ^ 24 different numbers [ipv6 is not considered for the time being], this memory is acceptable
In this way, we can use an array to traverse the complexity of O (n) to find the first 1000.

Finally, maintain a 1000 ordered array and insert data into it. if the number exceeds 1000, the minimum

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.