Forum Moderators: coopster

Message Too Old, No Replies

Storing numbers from a string, no arrays

         

StoutFiles

3:42 pm on Feb 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My brain just isn't working this morning and I need some help with this. I need to store the last three unique numbers in a string of numbers.

Let's say the numbers are 2, 3, 4, 4, 4, 4, 5, 5, 6, 6. I should have 6, 5, and 4 stored.

//reading in value
while(numbers_are_there)
{

//reading in value
}

Reading in the value is fine. This code is all psuedo, I'm writing it in Fortran but if I had a PHP one that worked I could easily convert. If I could get a psudeo answer I would be most appreciative.

coopster

3:49 pm on Feb 8, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would convert it to an array of unique values and pop off the last 3 values.

StoutFiles

3:52 pm on Feb 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Fortran 77 supports arrays but I'm not skilled enough with them to play around. Has to be something I can easily convert; therefore, no arrays. Trying to make it happen with well placed if-statements.

coopster

5:19 pm on Feb 8, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Hmmm. How about using the string length and substrings based on position of the comma within the string? Keep storing the last one read and if it matches one of your existing stored values, chop it off and discard it and read the next one. Your loop control would need to check for both 3 unique values as well as end of string. Just another thought.

StoutFiles

6:00 pm on Feb 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll provide some code for a better example. Replaced the reading in numbers from a file with a fixed array for the example. Replaced the while loop with a fixed for loop for the example.

<?php

$array = array(2, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7);

$first = $array[0];
$second = $array[0];
$third = $array[0];

for($i = 1; $i <=15; $i++)
{

$third = $second;
$second = $first;
$first = $array[$i];

}

echo "Final: <br>";
echo "First: ".$first."<br>";
echo "Second: ".$second."<br>";
echo "Third: ".$third."<br>";

?>

This example saves the last three values, but they are not unique. The answer I want returned for this example is:

First: 7
Second: 6
Third: 5


All work needs to be done in the for loop and with if-statements only. Extra variables can be added if they're needed for the if-statements.

coopster

5:03 pm on Feb 9, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I can do it using arrays in PHP, but do not remember enough FORTRAN any longer, been too many years for that!
<?php 
$string = '2, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7';
$array = array_unique(preg_split('/\D+/', $string));
$i = 0; // initialize a counter
$v = array(); // initialize a new array
while ($i++ < 3) {
$v[] = array_pop($array);
}
print '<pre>';
print_r($v);
print '</pre>';
exit;
?>

The_Dog

9:53 pm on Feb 10, 2011 (gmt 0)

10+ Year Member



Provided your data is nicely ordered, the following php code should work:

$array = array(2, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7);

$first = $array[0];
$second = $array[0];
$third = $array[0];

for($i = 1; $i <=15; $i++){
if ($array[$i] != $first and $array[$i] != $second and $array[$i] != $third){
$first = $second;
$second = $third;
$third = $array[$i];
}
}

echo "Final: <br>";
echo "First: ".$first."<br>";
echo "Second: ".$second."<br>";
echo "Third: ".$third."<br>";


If your input array is not sorted then results may vary. For Example:
2, 3, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 7, 4, 7 would print 6, 7, 4