Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WHILE loop still running when condition no longer met 1

Status
Not open for further replies.

PeDa

Technical User
Oct 10, 2002
227
NL
I have the following data (array with longitude values)
Code:
$Longitude[0] = -6.25195;
$Longitude[1] = -6.25192;
$Longitude[2] = -6.25192;
$Longitude[3] = -6.25177;
$Longitude[4] = -6.25241;
...

I want to generate an equivalent array containing letters, increasing throught the alphabet if a longitude value is not identical to the previous value. Thus (because longitudes [1] and [2] are identical):

Code:
$Letter[0] = 'A';
$Letter[1] = 'B';
$Letter[2] = 'B';
$Letter[3] = 'C';
$Letter[4] = 'D';
...

I have the following code (somewhat simplified):
Code:
$LastLongitude=0;
$NextLetter=-1;
$Looper = 0;
$CountHere = 1;

$i=0;
While ($i < $photoCount) 
  {
  if ($Longitude[$i] <> $LastLongitude) //next location
    {
    $Counter=1;
    $CountHere=1;
    $NextLetter=$NextLetter+1;
    $LastLongitude=$Longitude[$i]; 
    $Volgnummer[$i]=$Counter . "/" . $CountHere; 
    $Letter[$i]=substr('ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ',$NextLetter,1);
    $i=$i+1;
    }
  else  // another photo at this location
    {
    $CountHere=0;
    $j=$i;
    while ($Longitude[$j] == $LastLongitude) //how many more here?
      {
      $LastLatitude=$Latitude[$j]; 
      $LastLongitude=$Longitude[$j]; 
      $CountHere=$CountHere + 1;
      $j=$j + 1;
      if ($j==3)   // temporary kludge!!!!
        {
        echo $j . ': ' .$Longitude[$j] . ' x '. $LastLongitude;
        exit;
        }      
      }  
    for ($j=$i; $j<$i+$CountHere; $J++)
      {
      $Letter[$j]=substr('ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ',$NextLetter,1);
      $Volgnummer[$j]=$j . "/" . $CountHere; 
      $i=$i+1;
      }
    }
  }

The while block at
Code:
while ($Longitude[$j] == $LastLongitude) //how many more here?
should only process number [2], however if I leave out the kludge above the while loop just goes on ad infinitum until a time-out occurs. With the kludge I get:
Code:
echo $j . ': ' .$Longitude[$j] . ' x '. $LastLongitude;
3: -6.25177 x -6.25192

The WHILE loop appears to be still running when the condition is no longer being met; what am I doing wrong here?
 
Hi

Does it have to be that code ? Honestly I find it too complex. I would prefer this :
PHP:
[teal]<?php[/teal]

[navy]$Longitude[/navy][teal][[/teal][purple]0[/purple][teal]] = -[/teal][purple]6.25195[/purple][teal];[/teal]
[navy]$Longitude[/navy][teal][[/teal][purple]1[/purple][teal]] = -[/teal][purple]6.25192[/purple][teal];[/teal]
[navy]$Longitude[/navy][teal][[/teal][purple]2[/purple][teal]] = -[/teal][purple]6.25192[/purple][teal];[/teal]
[navy]$Longitude[/navy][teal][[/teal][purple]3[/purple][teal]] = -[/teal][purple]6.25177[/purple][teal];[/teal]
[navy]$Longitude[/navy][teal][[/teal][purple]4[/purple][teal]] = -[/teal][purple]6.25241[/purple][teal];[/teal]

[navy]$Letter[/navy] [teal]= [];[/teal]

[navy]$NextLetter[/navy] [teal]=[/teal] [b]null[/b][teal];[/teal]
[navy]$LastLongitude[/navy] [teal]=[/teal] [b]null[/b][teal];[/teal]
[b]foreach[/b] [teal]([/teal][navy]$Longitude[/navy] [b]as[/b] [navy]$key[/navy] [teal]=>[/teal] [navy]$value[/navy][teal]) {[/teal]
	[navy]$Letter[/navy][teal][[/teal][navy]$key[/navy][teal]] =[/teal] [navy]$LastLongitude[/navy] [teal]==[/teal] [navy]$value[/navy] [teal]?[/teal] [navy]$NextLetter[/navy] [teal]: ([/teal][navy]$NextLetter[/navy] [teal]? ++[/teal][navy]$NextLetter[/navy] [teal]:[/teal] [navy]$NextLetter[/navy] [teal]=[/teal] [i][green]'A'[/green][/i][teal]);[/teal]
	[navy]$LastLongitude[/navy] [teal]=[/teal] [navy]$value[/navy][teal];[/teal]
[teal]}[/teal]

[COLOR=orange]print_r[/color][teal]([/teal][navy]$Letter[/navy][teal]);[/teal]


Feherke.
feherke.github.io
 
Thank you for this elegant suggestion. Unfortunately (shown in my code, but not mentioned in my text), I also have to calculate a counter ('1/1', '1/2', '2/2' etc) for each item at each location:
Code:
$Volgnummer[$i]=$Counter . "/" . $CountHere;
 
Hi

Unsure whether I got it correctly. If the numbers mean "n[sup]th[/sup] of m", then I would do it in a separate loop over the 1[sup]st[/sup] loop's results :
PHP:
[small][teal]<?php[/teal]

[navy]$Longitude[/navy][teal][[/teal][purple]0[/purple][teal]] = -[/teal][purple]6.25195[/purple][teal];[/teal]
[navy]$Longitude[/navy][teal][[/teal][purple]1[/purple][teal]] = -[/teal][purple]6.25192[/purple][teal];[/teal]
[navy]$Longitude[/navy][teal][[/teal][purple]2[/purple][teal]] = -[/teal][purple]6.25192[/purple][teal];[/teal]
[navy]$Longitude[/navy][teal][[/teal][purple]3[/purple][teal]] = -[/teal][purple]6.25177[/purple][teal];[/teal]
[navy]$Longitude[/navy][teal][[/teal][purple]4[/purple][teal]] = -[/teal][purple]6.25241[/purple][teal];[/teal]

[navy]$Letter[/navy] [teal]= [];[/teal]

[navy]$NextLetter[/navy] [teal]=[/teal] [b]null[/b][teal];[/teal]
[navy]$LastLongitude[/navy] [teal]=[/teal] [b]null[/b][teal];[/teal]
[b]foreach[/b] [teal]([/teal][navy]$Longitude[/navy] [b]as[/b] [navy]$key[/navy] [teal]=>[/teal] [navy]$value[/navy][teal]) {[/teal]
    [navy]$Letter[/navy][teal][[/teal][navy]$key[/navy][teal]] =[/teal] [navy]$LastLongitude[/navy] [teal]==[/teal] [navy]$value[/navy] [teal]?[/teal] [navy]$NextLetter[/navy] [teal]: ([/teal][navy]$NextLetter[/navy] [teal]? ++[/teal][navy]$NextLetter[/navy] [teal]:[/teal] [navy]$NextLetter[/navy] [teal]=[/teal] [i][green]'A'[/green][/i][teal]);[/teal]
    [navy]$LastLongitude[/navy] [teal]=[/teal] [navy]$value[/navy][teal];[/teal]
[teal]}[/teal]

[COLOR=orange]print_r[/color][teal]([/teal][navy]$Letter[/navy][teal]);[/teal]

[gray]// no change above this line[/gray][/small]

[navy]$Volgnummer[/navy] [teal]= [];[/teal]

[navy]$count[/navy] [teal]=[/teal] [COLOR=orange]array_count_values[/color][teal]([/teal][navy]$Letter[/navy][teal]);[/teal]

[navy]$CountHere[/navy] [teal]=[/teal] [purple]1[/purple][teal];[/teal]
[navy]$LastLetter[/navy] [teal]=[/teal] [b]null[/b][teal];[/teal]
[b]foreach[/b] [teal]([/teal][navy]$Letter[/navy] [b]as[/b] [navy]$key[/navy] [teal]=>[/teal] [navy]$value[/navy][teal]) {[/teal]
    [navy]$Volgnummer[/navy][teal][[/teal][navy]$key[/navy][teal]] = ([/teal][navy]$LastLetter[/navy] [teal]==[/teal] [navy]$value[/navy] [teal]? ++[/teal][navy]$CountHere[/navy] [teal]:[/teal] [navy]$CountHere[/navy] [teal]=[/teal] [purple]1[/purple][teal]) .[/teal] [i][green]'/'[/green][/i] [teal].[/teal] [navy]$count[/navy][teal][[/teal][navy]$value[/navy][teal]];[/teal]
    [navy]$LastLetter[/navy] [teal]=[/teal] [navy]$value[/navy][teal];[/teal]
[teal]}[/teal]

[COLOR=orange]print_r[/color][teal]([/teal][navy]$Volgnummer[/navy][teal]);[/teal]


Feherke.
feherke.github.io
 
Yes; this is how it should be. Many thanks indeed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top