The online racing simulator
Problem with FOREACH
Either there's a problem with FOREACH in Lapper or I am missing a trick.
See code below.
The dumpVar in Sub 'testarr' is displaying the array that is filled on the start of Lapper.
But then the FOREACH-loop should start and for some reason that isn't happening.
I had it in a normal sub first, and now put it in a sub started by typing: !testarr
What am I missing here or has this loop-function gone broken at some point?


CatchEvent OnLapperStart()
OnLapperStart_Test();
EndCatchEvent

Sub OnLapperStart_Test()
GlobalVar $number_array;
GlobalVar $new_number_array;

$number_array[0]=1;
$number_array[1]=2;
$number_array[2]=3;
$number_array[3]=4;
$number_array[4]=5;
$number_array[5]=6;
$number_array[6]=7;
$number_array[7]=8;
EndSub

CatchEvent OnMSO( $userName, $text ) # Player event
$idxOfFirtsSpace = indexOf( $text, " ");

IF( $idxOfFirtsSpace == -1 ) THEN
$command = $text;
$argv = "";
ELSE
$command = subStr( $text,0,$idxOfFirtsSpace );
$argv = trim( subStr( $text,$idxOfFirtsSpace ) );
ENDIF

SWITCH( $command )
CASE "!testarr":
testarr ( $KeyFlags , $argv );
BREAK;
ENDSWITCH
EndCatchEvent

Sub testarr( $KeyFlags,$id )
dumpVar ($number_array);
$counter=0;
FOREACH( $val IN $number_array)
writeline ( "DEBUG ARR: " . $val["value"] );
IF ($val["value" ] != 3)
THEN
$new_number_array[$counter] = $val["value" ];
ENDIF

$counter=$counter+1;
ENDFOREACH

dumpVar ($new_number_array);
EndSub

i might found an solution, which is very stupid i think:

I just created a new Var for the array, inside the testarr sub-routine

CatchEvent OnLapperStart()
OnLapperStart_Test();
EndCatchEvent

Sub OnLapperStart_Test()
GlobalVar $number_array;
GlobalVar $new_number_array;

$number_array[0] = 1;
$number_array[1] = 2;
$number_array[2] = 3;
$number_array[3] = 4;
$number_array[4] = 5;
$number_array[5] = 6;
$number_array[6] = 7;
$number_array[7] = 8;
EndSub

CatchEvent OnMSO( $userName, $text ) # Player event
$idxOfFirtsSpace = indexOf( $text, " ");

IF( $idxOfFirtsSpace == -1 ) THEN
$command = $text;
$argv = "";
ELSE
$command = subStr( $text,0,$idxOfFirtsSpace );
$argv = trim( subStr( $text,$idxOfFirtsSpace ) );
ENDIF

SWITCH( $command )
CASE "!testarr":
testarr ( $KeyFlags , $argv );
BREAK;
ENDSWITCH
EndCatchEvent

Sub testarr( $KeyFlags,$id )

####NEW VAR####
$NewArray = $number_array;

dumpVar ($NewArray);
$counter=0;
FOREACH( $val IN $NewArray)
writeline ( "DEBUG ARR: " . $val["value"] );
IF ($val["value" ] != 3)
THEN
$new_number_array[$counter] = $val["value" ];
ENDIF

$counter=$counter+1;
ENDFOREACH

dumpVar ($new_number_array);
EndSub

That's very weird, but if it works, it works Smile
Just tested and it does work.
Any chance you can figure out why that's needed?
Other request would be able to unset one value from the array, instead of the whole array using the unset feature.
My test-script had two bugs, which are adjusted in the code below (in case anyone might be interested to use it):


CatchEvent OnLapperStart()
OnLapperStart_Test();
EndCatchEvent

Sub OnLapperStart_Test()
GlobalVar $number_array;
GlobalVar $new_number_array;

$number_array[0]=1;
$number_array[1]=2;
$number_array[2]=3;
$number_array[3]=4;
$number_array[4]=5;
$number_array[5]=6;
$number_array[6]=7;
$number_array[7]=8;
EndSub

CatchEvent OnMSO( $userName, $text ) # Player event
$idxOfFirtsSpace = indexOf( $text, " ");

IF( $idxOfFirtsSpace == -1 ) THEN
$command = $text;
$argv = "";
ELSE
$command = subStr( $text,0,$idxOfFirtsSpace );
$argv = trim( subStr( $text,$idxOfFirtsSpace ) );
ENDIF

SWITCH( $command )
CASE "!testarr":
testarr ( $KeyFlags , $argv );
BREAK;
ENDSWITCH
EndCatchEvent

Sub testarr( $KeyFlags,$id )
$NewArray = $number_array;
dumpVar ($NewArray);
$counter=0;
FOREACH( $val IN $NewArray)
writeline ( "DEBUG ARR: " . $val["value"] );
IF ($val["value" ] != 3)
THEN
$new_number_array[$counter] = "" . $val["value" ];
$counter=$counter+1;
ENDIF
ENDFOREACH

dumpVar ($new_number_array);
EndSub

Well what i think it works the same as.

$ListOfPlayers = getListOfPlayers("U");

Because getListOfPlayers() is an array aswell.

I have to study the sourcecode even more to understand it.

I will figure out, how to unset 1 value in an array. I think, i did this once in one of my scripts before. but cant remember which one :S Could be the sortscript.

Heres the code i have used for sorting the times in the Timetable of my Timeattack addon.

Maybe its usefull or not. Sorry for the lack of commentary

Just a note for all readers who copied this code: This code simply doesnt work , because it is connected with other parts of the addon, dont ask for the rest of the code, you will not get it



Sub SortArray($DisplayedStage)
################Sort Times###############
#Add a '0' to the number to create a 2 to 7 digit number
#This will also converts string to Numeric character
UserGroupFromFile( "RallyUserTime".$DisplayedStage,"includes/RallySystem/Times/Times".$DisplayedStage.".txt" ); # Load File
$ListOfTimes = GetListOfUsersGroup("RallyUserTime".$DisplayedStage); #Which stage has to be displayed
$Count_GetNumber = arrayCount( $ListOfTimes );
FOR ($i=0;$i<=$Count_GetNumber;$i=$i+1)
$GetDataFromLine = SplitToArray($ListOfTimes[$i],"|");
IF (IsNum($GetDataFromLine[4])== TRUE) THEN
### Array with values from 0 - 10 ###
IF ((ToNum($GetDataFromLine[4]) > 0) && (ToNum($GetDataFromLine[4]) < 10)) THEN
$GetData[$i,4] = "000000".$GetDataFromLine[4];
ENDIF
### Array with values from 9 - 100 ###
IF ((ToNum($GetDataFromLine[4]) > 9) && (ToNum($GetDataFromLine[4]) < 100)) THEN
$GetData[$i,4] = "00000".$GetDataFromLine[4];
ENDIF
### Array with values from 99 - 1000 ###
IF ((ToNum($GetDataFromLine[4]) > 99) && (ToNum($GetDataFromLine[4]) < 1000)) THEN
$GetData[$i,4] = "0000".$GetDataFromLine[4];
ENDIF
### Array with values from 999 - 10000 ###
IF ((ToNum($GetDataFromLine[4]) > 999) && (ToNum($GetDataFromLine[4]) < 10000)) THEN
$GetData[$i,4] = "000".$GetDataFromLine[4];
ENDIF
### Array with values from 9999 - 100000 ###
IF ((ToNum($GetDataFromLine[4]) > 9999) && (ToNum($GetDataFromLine[4]) < 100000)) THEN
$GetData[$i,4] = "00".$GetDataFromLine[4];
ENDIF
### Array with values from 99999 - 1000000 ###
IF ((ToNum($GetDataFromLine[4]) > 99999) && (ToNum($GetDataFromLine[4]) < 1000000)) THEN
$GetData[$i,4] = "0".$GetDataFromLine[4];
ENDIF
$GetData[$i,1] = $GetDataFromLine[1].""; #String (UserName)
$GetData[$i,2] = $GetDataFromLine[2].""; #String (NickName)
$GetData[$i,3] = $GetDataFromLine[3].""; #String (Car)
$GetData[$i,5] = $GetDataFromLine[5].""; #Num (Penalty)
$GetData[$i,6] = $GetDataFromLine[6].""; #Num (maxSpeed)
$GetData[$i,7] = $GetDataFromLine[7].""; #String (Splittimes)
ENDIF
ENDFOR

$SaveString_One = ""; #String (NickName)
$SaveString_Two = ""; #String (NickName)
$SaveString_Three = ""; #String (Car)
$SaveNumber_Four = 0; #Num (Time)
$SaveNumber_Five = 0; #Num (Penalty)
$SaveNumber_Six = 0; #Num (maxSpeed)
$SaveNumber_Seven = 0; #String (maxSpeed)
#######################
###Ascending sorting###
#######################
FOR ($LineOfFileOne=0;$LineOfFileOne<=$Count_GetNumber;$LineOfFileOne=$LineOfFileOne+1)
FOR ($LineOfFileTwo=$LineOfFileOne;$LineOfFileTwo<=$Count_GetNumber;$LineOfFileTwo=$LineOfFileTwo+1)
IF (($GetData[$LineOfFileOne,4]) > ($GetData[$LineOfFileTwo,4])) THEN

#Temperary Saved vars
$SaveString_One = $GetData[$LineOfFileTwo,1].""; #String (NickName)
$SaveString_Two = $GetData[$LineOfFileTwo,2].""; #String (NickName)
$SaveString_Three = $GetData[$LineOfFileTwo,3].""; #String (Car)
$SaveNumber_Four = $GetData[$LineOfFileTwo,4].""; #Num (Time)
$SaveNumber_Five = $GetData[$LineOfFileTwo,5].""; #Num (Penalty)
$SaveNumber_Six = $GetData[$LineOfFileTwo,6].""; #Num (MaxSpeed)
$SaveNumber_Seven = $GetData[$LineOfFileTwo,7].""; #String (Splits)

$GetData[$LineOfFileTwo,1] = $GetData[$LineOfFileOne,1]."";
$GetData[$LineOfFileTwo,2] = $GetData[$LineOfFileOne,2]."";
$GetData[$LineOfFileTwo,3] = $GetData[$LineOfFileOne,3]."";
$GetData[$LineOfFileTwo,4] = $GetData[$LineOfFileOne,4]."";
$GetData[$LineOfFileTwo,5] = $GetData[$LineOfFileOne,5]."";
$GetData[$LineOfFileTwo,6] = $GetData[$LineOfFileOne,6]."";
$GetData[$LineOfFileTwo,7] = $GetData[$LineOfFileOne,7]."";

$GetData[$LineOfFileOne,1] = $SaveString_One."";
$GetData[$LineOfFileOne,2] = $SaveString_Two."";
$GetData[$LineOfFileOne,3] = $SaveString_Three."";
$GetData[$LineOfFileOne,4] = $SaveNumber_Four."";
$GetData[$LineOfFileOne,5] = $SaveNumber_Five."";
$GetData[$LineOfFileOne,6] = $SaveNumber_Six."";
$GetData[$LineOfFileOne,7] = $SaveNumber_Seven."";
ENDIF
ENDFOR
ENDFOR

##########################################################
#FILTER CARS OR PLAYERS
##########################################################
#Save sorted array to List
IF ((GetCurrentPlayerVar("RallyTimeTableFilterCar") == "")&&(GetCurrentPlayerVar("RallyTimeTableFilterUserName") == "")) THEN
FOR ($i=0;$i<=$Count_GetNumber;$i=$i+1)
$showlist[ToNum($i),1] = $GetData[$i,1]."";
$showlist[ToNum($i),2] = $GetData[$i,2]."";
$showlist[ToNum($i),3] = $GetData[$i,3]."";
$showlist[ToNum($i),4] = $GetData[$i,4]."";
$showlist[ToNum($i),5] = $GetData[$i,5]."";
$showlist[ToNum($i),6] = $GetData[$i,6]."";
$showlist[ToNum($i),7] = $GetData[$i,7]."";
ENDFOR
ELSE
#Something must be Filtered
$showline = 1;
#Clear data from previous array
FOR ($Line=0;$Line<=$Count_GetNumber;$Line=$Line+1)
#Clear Arrays
$showlist[ToNum($Line),1] = "";
$showlist[ToNum($Line),2] = "";
$showlist[ToNum($Line),3] = "";
$showlist[ToNum($Line),4] = "";
$showlist[ToNum($Line),5] = "";
$showlist[ToNum($Line),6] = "";
$showlist[ToNum($Line),7] = "";
ENDFOR
#Car Filter
IF (GetCurrentPlayerVar("RallyTimeTableFilterCar") != "") THEN
$CarsString = SplitToArray(GetCurrentPlayerVar("RallyTimeTableFilterCar"),"+");
$CarsCount = arrayCount($CarsString);
FOR ($c=0;$c<=$CarsCount;$c=$c+1)
FOR ($Car=1;$Car<=20;$Car=$Car+1)
FOR ($Line=0;$Line<=$Count_GetNumber;$Line=$Line+1)
IF ((ToUpper($GetData[ToNum($Line),3]) == ToUpper($CarsString[$c]))&&($Car_TimeTable[$Car] == ToUpper($CarsString[$c]))) THEN
$showlist[ToNum($showline),1] = $GetData[ToNum($Line),1]."";
$showlist[ToNum($showline),2] = $GetData[ToNum($Line),2]."";
$showlist[ToNum($showline),3] = $GetData[ToNum($Line),3]."";
$showlist[ToNum($showline),4] = $GetData[ToNum($Line),4]."";
$showlist[ToNum($showline),5] = $GetData[ToNum($Line),5]."";
$showlist[ToNum($showline),6] = $GetData[ToNum($Line),6]."";
$showlist[ToNum($showline),7] = $GetData[ToNum($Line),7]."";
$showline = $showline+1;
ENDIF
ENDFOR
ENDFOR
ENDFOR
ENDIF
#PlayerName Filter
IF (GetCurrentPlayerVar("RallyTimeTableFilterUserName") != "") THEN
$PlayerString = SplitToArray(GetCurrentPlayerVar("RallyTimeTableFilterUserName"),"+");
$PlayerCount = arrayCount($PlayerString);
FOR ($p=0;$p<=$PlayerCount;$p=$p+1)
FOR ($Line=0;$Line<=$Count_GetNumber;$Line=$Line+1)
IF (ToLower($GetData[ToNum($Line),1]) == ToLower($PlayerString[$p])) THEN
$showlist[ToNum($showline),1] = $GetData[ToNum($Line),1]."";
$showlist[ToNum($showline),2] = $GetData[ToNum($Line),2]."";
$showlist[ToNum($showline),3] = $GetData[ToNum($Line),3]."";
$showlist[ToNum($showline),4] = $GetData[ToNum($Line),4]."";
$showlist[ToNum($showline),5] = $GetData[ToNum($Line),5]."";
$showlist[ToNum($showline),6] = $GetData[ToNum($Line),6]."";
$showlist[ToNum($showline),7] = $GetData[ToNum($Line),7]."";
$showline = $showline+1;
ENDIF
ENDFOR
ENDFOR
ENDIF
ENDIF

#############################
#Set Fastest/Slowest Time
#############################
$WorstTime = 0;
$BestTime = 999999;
FOR ($Line=0;$Line<=$Count_GetNumber;$Line=$Line+1)
IF( IsNum($showlist[ToNum($Line),4]) == TRUE) THEN
IF (ToNum($BestTime) > ToNum($showlist[ToNum($Line),4])) THEN #FASTER THAN
$BestTime = $showlist[ToNum($Line),4]; #Save Current BestTime
ENDIF
IF (ToNum($WorstTime) < ToNum($showlist[ToNum($Line),4])) THEN #SLOWER THAN
$WorstTime = $showlist[ToNum($Line),4]; #Save Current WorstTime
ENDIF
ENDIF
ENDFOR

DisplayList(Getcurrentplayervar("RallyTimeStage"),getcurrentplayervar("RallyTimeTablePage")*15);
EndSub


FGED GREDG RDFGDR GSFDG