Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Mar 2018
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Find largest number in a text file?

    I'm trying to return the largest number in a text file, and I've worked the code down to this:

    PHP Code:
    <?php
    $original 
    file('input.txt');
    $match max(preg_grep('/[0-9]{0,15}$/'$original));
    echo 
    $match;
    ?>
    But no matter if my text file contains 1000 or 1000000 ... the largest number it ever returns is 9. What am I missing here?

    Code:
    <span name="7"></span>
    <span name="8"></span>
    <span name="9"></span>
    <span name="10"></span>
    <span name="11"></span>
    <span name="10000"></span>

  2. #2
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,668
    Thanks
    24
    Thanked 580 Times in 573 Posts
    What am I missing here?
    you're comparing tags, not numbers. and <span name="9 is larger than <span name="1 (no matter if there are more numbers after the 1).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  3. Users who have thanked Dormilich for this post:

    kilo_b (Mar 14th, 2018)

  4. #3
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    2,923
    Thanks
    3
    Thanked 415 Times in 404 Posts
    @dormlich has it right in that you're pulling the whole string thanks to that $, but you're not telling the regex to check for more than ZERO characters... so ANY string would be valid.

    Really though:

    /[0-9]+/

    Would be all I'd be using for a regex... well, unless you want the whole line in which case you might have to explode after the regex and brute force the check.

    Even then, it might not work as expected thanks to PHP's derpy loose typecasting. String sorting != numeric sorting, and that preg_grep is likely filling the array with STRINGS... which means 9 > 10. That would hinge on preg_grep's implementation, but that's something that bites people in JavaScript ALL the time.
    Last edited by deathshadow; Mar 13th, 2018 at 02:30 PM.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  5. Users who have thanked deathshadow for this post:

    kilo_b (Mar 14th, 2018)

  6. #4
    New Coder
    Join Date
    Mar 2018
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I think I understand what you're both saying. I'm struggling to find a solution though. Any suggestions?

  7. #5
    New to the CF scene
    Join Date
    Mar 2018
    Location
    London
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why not parse the string and check the value of what's between the delimiters?

  8. #6
    New Coder
    Join Date
    Mar 2018
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hmm, good idea. I tried this, but instead of "11", I get "67891011". Help?

    PHP Code:
    <?php
    $original 
    file('input.txt');

    foreach(
    $original as $line) {
      
    $match substr($line030);
      
    $strip filter_var($matchFILTER_SANITIZE_NUMBER_INT);
      
    $output = array($strip);
      echo 
    max($strip);
    }

    ?>
    I'm using substr($line, 0, 30); because each line has the number twice - so I only want the first 30 characters of each.

    Code:
    <div name="6" class="blahblahblah">contentgoeshere<a href="delete.php?id=6" class="blah">-</a></div>

  9. #7
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    2,923
    Thanks
    3
    Thanked 415 Times in 404 Posts
    Oh, duh... I feel silly. preg_grep returns the entire line IF there's a match, not the match. As such in your case the array you put in would be identical to the one you get out!

    You'd have to brute force this, at which point just keep match and the memory wasting result array out of it.

    Code:
    $highest = 0;
    
    foreach ($original as $line) if (
    	preg_match('/\d+/', $line, $value) &&
    	$value[0] > $highest
    ) $highest = $value[0];
    
    echo $highest;
    To that end I would consider reading the file one line at a time instead of wasting memory on having it all in there at once. TRY not to make copies of things in memory you don't have to!

    Also preg_match only returns the FIRST match in the string, so you don't have to play with truncating the length.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  10. Users who have thanked deathshadow for this post:

    kilo_b (Mar 14th, 2018)

  11. #8
    New Coder
    Join Date
    Mar 2018
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts
    That works, thank you! Now I just need to understand how it works so I can improve.

    Quote Originally Posted by deathshadow View Post
    Oh, duh... I feel silly.
    You feel silly? I feel silly. I'm just learning PHP, but I think I'm approaching it ... sideways. Like asking engineering questions from a chef.

  12. #9
    New Coder
    Join Date
    Dec 2017
    Location
    Cincinnati,Ohio,USA
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can use Max() function in php.
    Syntax

    max(array_values);

    or

    max(value1,value2,...);

    for example:

    <?php
    echo(max(2,4,6,8,10) . "<br>");
    echo(max(22,14,68,18,15) . "<br>");
    echo(max(array(4,6,8,10)) . "<br>");
    echo(max(array(44,16,81,12)));
    ?>

  13. #10
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    2,923
    Thanks
    3
    Thanked 415 Times in 404 Posts
    Quote Originally Posted by theninehertz View Post
    You can use Max() function in php.
    He was, the array is non-numerical since it's markup. Attempts to pull an array of values doesn't work since that's not what preg_grep does... so if you're going to walk them one at a time (which me must) you might as well NOT even get arrays involved since building a result array is just a waste of memory.

    You might want to bother reading posts before responding -- your response was completely irrelevant to the problem.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •