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 13 of 13
  1. #1
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Order numerically

    I have a text file, with a bunch of numbers in numerical order from greatest to least, I need it to add the specified number to the file so it fits into the file so the file still remains from greatest to least, I also need to know what line it added it on. I dont really know how to do this besides writing it out like:
    PHP Code:
    $files file("scores.txt");
    if((
    $num $files[1])&&($num $files[2])){
    $file fopen("scores.txt"w);
    fputs($file$file[0]);
    fputs($file$file[1]);
    fputs($file$num);
    fputs($file$file[2]);
    fputs($file$file[3]);
    fclose($file); 
    Now that owuld take along time, so isn't their an easier way to do it?

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    can't you use a db instead of a textfile?

    with a textfile, the file will be locked during the complete operations so if it's a heavily used file, this will increase responsetimes.
    I think your only options would be to dfirst dump the complete file into an array, that is build like this

    $array[x][y]
    where x is the linenumber (so each time a new line is read from the txt, you add a new element to $array[])
    y = a value from line x
    You'll need to compaire eache value to see if it's higher then the value you want to insert. If it's not, then you add the value to $array[x][]. If it is higher, then you fist add your nex value to $array[x][] and then set a flag. From then on, all values can be added directly into the array.(or, depending on how the linecuts are decided, you can stop processing the file, rebuild the new filecontent and write it back)

    But it's realy not the sort of functionality a txt file is intended for. using a db will be much faster.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ah, I came up with this much, it puts it into the file, then sorts it, when it reads it, it puts it in order:
    PHP Code:
    $str implode'' file"scores.txt" ) ) ; 
    $newline "\n" $newline;
    $str2 implode'' file"player.txt" ) ) ; 
    $num count(file("Scores.txt"));
    $file fopen("scores.txt""w");
    fputs($file$_POST['score']);
    fputs($file$newline);
    fputs($file$str);
    fclose($file);
    $file2 fopen("player.txt""w");
    fputs($file2$_POST['name']);
    fputs($file2$newline);
    fputs($file2$str2);
    fclose($file2); 
    but it doesn't keep the name along with it, how can I get it to keep the name?

    btw my server only supports php3, so krsort isn't supported, if you know of a server that supports php4 or something that is free feel free to suggest

  • #4
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    ok, here is a rather rough-hammer-it approach that may help - the major problem is that it does not account for duplicate scores (it stops searching at the first occurrence)

    Code:
    <?PHP
    // put the file into an array
    // using a simple setup where the lines of the file are score and team name separated by the pipe |
    
    $all_lines = file("scores.txt");
    
    // add the new value to the array
    $new_value = "9|tuna";
    $hold = explode("|", $new_value);
    $new_score = $hold[0];
    $total = array_push($all_lines, $new_value);
    
    // count the number of lines
    $num_lines = count($all_lines);
    
    // pad the score with leading 0's
    $pad0 = "000000000000000000000";
    $i=0;
    for ($i = 0; $i < $num_lines; $i ++) {  $all_lines[$i] = $pad0 . $all_lines[$i];}
    // make the scores the same length
    $i = 0;
    for ($i - 0; $i < $num_lines; $i ++) {
      $x1 = explode("|", $all_lines[$i]);
      $x1[0] = substr($x1[0], -5);
      $all_lines[$i] = implode("|", $x1);
    }
    
    // sort by score
    sort($all_lines);
    
    // remove the leading 0's
    $i = 0;
    for ($i - 0; $i < $num_lines; $i ++) {
      $x1 = explode("|", $all_lines[$i]);
      $x1[0] = $x1[0] * 1;
      $all_lines[$i] = implode("|", $x1);
    }
    
    
    function binary_search($array, $element) {
    	/** Returns the position of found $element or x */
    	$low = 0;
    	$high = count($array) - 1;
    	while ($low <= $high) { 
    		$mid = floor(($low + $high) / 2);  // C floors for you
    
    																					$x1 = explode("|", $array[$mid]);
    		if ($element == $x1[0]) {
    			return $mid;
    		}
    		else {
    			if ($element < $x1[0]) {
    				$high = $mid - 1;
    			}
    			else {
    				$low = $mid + 1;
    			}
    		}
    	}
    	return "x";  // $element not found
    }
    
    if (binary_search($all_lines, $new_score) == 'x') {
      //value not found take appropriate action
      echo "not found";
    } else {
      echo binary_search($all_lines, $new_score) . "  is the array position <br>";
      echo "Whereas " . (binary_search($all_lines, $new_score) + 1) . "  is the line number <br>";
    }
    /* foreach($all_lines as $value) {
     print "$value " . "<br>"; 
    } 
    */
    
    ?>
    it might spark some ideas for you.

    Lite...

  • #5
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I dont really understand that, that is above and beyond my php skills. So I guess I cant really use it, unless you program the same score thing in their, or even better you can explain how it works, though I do think I understand how it works, I just wouldn't know how to program it, but I can read it, I suppose you can understand that.

  • #6
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry to double post, but I tested out your code, every time it returns:
    2 is the array position
    Whereas 3 is the line number
    and I configured it so that "tuna" had a much higher score

    which is kinda weird, because it doesn't look like it would do that...

  • #7
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    oops I forgot to give you MY score.txt file. Obviously putting 9|tuna in your file would not work. so here is the content of my file - note I use the pipe '|' as the separator.

    remember the first element in an array is called 0, even tho it is line number 1
    the text file
    13|sharks
    21|minnows
    4|barracudas
    7|bass
    5|swordfish
    6|pike
    12|trout

  • #8
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ...

    kk, I guess that makes sense, your scores.txt was in different format so that was the problem, and I after examining the code I understand everything basically except the binary_search, oh well...

    Edit:
    After putting the text file it still always returns that text I told you, does anybody else have some insight on why it does this?
    Last edited by stophon4; 05-23-2004 at 06:27 PM.

  • #9
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    it returns that wording because the last part of the script is simply an example
    Code:
    if (binary_search($all_lines, $new_score) == 'x') {
      //value not found take appropriate action
      echo "not found";
    } else {
      echo binary_search($all_lines, $new_score) . "  is the array position <br>";
      echo "Whereas " . (binary_search($all_lines, $new_score) + 1) . "  is the line number <br>";
    }
    /* foreach($all_lines as $value) {
     print "$value " . "<br>"; 
    }
    Without seeing your actual text file and not knowing what you want to do with the results, I simply made it an example. You will need to tweak it to meet your needs.

    As for the Binary Search. It is a method reducing search time. First it looks in the 1st 1/2 of the file. If it is not there then it looks in the 2nd half, tottaly ignoring the first 1/2 from then on. It keeps halving the remaing portion until it is found. Clear as mud?

    Lite...
    Last edited by litebearer; 05-23-2004 at 06:58 PM.

  • #10
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    About as clear as mud, if not more muddy, and I mean it is about as clear as mud...

    All I really need to do is konw what line to put it on (the array number), and I modifyed it to use the same format you used for your text file, but the text file will be changing every time that is evaluated...

  • #11
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    Generally, what line information is on is insignificant. All we really care about is can we find and access the information when we need it.

    So, if you would, post the first 5 lines of your text file and I will make as compact a script as I can.

    The script will do three things:

    1. Add the new score/variable to the file
    2. put the file in order from greatest to least.
    3. tell you what line the new score/variable is on (this may change as you add new scores - in order words if score 12 is on line 5 today and you add a new score tomorrow of 13, them the score of 12 will move down to line 6)

  • #12
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    10000|Stophon4
    7500|Someone
    5000|Freddy
    2500|cooldude
    0|Thedude
    THere's the text file, it also needs to be written in that format

  • #13
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have done some work, and I have a new idea on how to work this, I need to sort the replays by the last six numbers of the array, the first part of it will be some letters with variable length, and then a | and then I need it to sort by the numbers, I just need to know how to sort by last 6 characters of an array....


  •  

    Posting Permissions

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