AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Read file backwards (https://forums.alliedmods.net/showthread.php?t=319153)

iceeedr 10-15-2019 13:34

Read file backwards
 
Hello AM, I populate a menu with data from an .ini file, however, I would like to populate the menu with data from Back to Front (newer to older), but I found nothing about...

Thanks.

JocAnis 10-15-2019 13:59

Re: Read file backwards
 
store all lines into array, then add all items from the array from the LAST added, going descending...? you will know that to make bro :o

AmXDusT 10-16-2019 17:55

Re: Read file backwards
 
Not sure if that would work, but you can try recursion

Bugsy 10-16-2019 18:16

Re: Read file backwards
 
It would work and is the simplest way to handle this, IMO.

iceeedr 10-16-2019 18:18

Re: Read file backwards
 
What "recursion" is? My english didn't figure out what you're trying to tell me haha

Bugsy 10-16-2019 18:30

Re: Read file backwards
 
Don't listen to him, just do as JocAnis suggested, this is not that complicated.

EFFx 10-16-2019 23:02

Re: Read file backwards
 
Simplesmente, você le o arquivo normalmente de cima pra baixo e quando você for registrar o menu, comece do ultimo, pegando os valores decrescentes. Tipo, normalmente se utiliza o loop usando ++, é só você trocar pro --, tipo assim:

PHP Code:

// "i" will start with the MAX_VALUE readed, the loop will continue 'til the "i" be equal to or less than 0.
for(new ArraySize(g_arIniFileData);0;i--) 

E provavelmente, quando você for registrar o menu, mesmo usando o loop ao contrário, quando você for selecionar a opção ele ainda vai começar do 0 ao valor maximo que você registrar, então é necessário você passar a ID do item também pro handler, assim você pode saber que opção EXATA o jogador vai selecionar.

Basically:

Simply, you read the file normally, and when you'd create the menu, use the loop backwards, instead of increasing, you decrese the value, something like I showed. And probably, when you do register the menu, even using the reverse loop, after you select the option it'll still start from the 0 to the max value you registered without the menu_get_iteminfo, so you'll need to pass the ID of the item to the menu handler, with this, you'll be able to know which EXACT option the player would select.

Bugsy 10-16-2019 23:17

Re: Read file backwards
 
Quote:

Originally Posted by EFFx (Post 2669932)
Simplesmente, você le o arquivo normalmente de cima pra baixo e quando você for registrar o menu, comece do ultimo, pegando os valores decrescentes. Tipo, normalmente se utiliza o loop usando ++, é só você trocar pro --, tipo assim:

PHP Code:

// "i" will start with the MAX_VALUE readed, the loop will continue 'til the "i" be equal to or less than 0.
for(new ArraySize(g_arIniFileData);0;i--) 

E provavelmente, quando você for registrar o menu, mesmo usando o loop ao contrário, quando você for selecionar a opção ele ainda vai começar do 0 ao valor maximo que você registrar, então é necessário você passar a ID do item também pro handler, assim você pode saber que opção EXATA o jogador vai selecionar.

Basically:

Simply, you read the file normally, and when you'd create the menu, use the loop backwards, instead of increasing, you decrese the value, something like I showed. And probably, when you do register the menu, even using the reverse loop, after you select the option it'll still start from the 0 to the max value you registered without the menu_get_iteminfo, so you'll need to pass the ID of the item to the menu handler, with this, you'll be able to know which EXACT option the player would select.

for(new i = ArraySize(g_arIniFileData);i > 0;i--)

should be

for(new i = ArraySize(g_arIniFileData) - 1 ; i >= 0 ;i--)

or you will get an index out of bounds on the first pass using g_arIniFileData[ i ] and you also want to allow 0 to be accessed so >= 0 should be used.

iceeedr 10-17-2019 06:32

Re: Read file backwards
 
It worked, thanks to everyone who helped me, my mistake was at the end of the loop, because instead of decreasing I was adding value (inattention).

Natsheh 10-17-2019 07:50

Re: Read file backwards
 
Hello you also could use fseek

More info from the documentation

Spoiler


All times are GMT -4. The time now is 20:29.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.