Raised This Month: $ Target: $400
 0% 

Solved Prevent double looping


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 07-16-2018 , 11:33   Re: Prevent double looping
Reply With Quote #1

I don't even understand what you mean by "double looping", nested loops are a normal thing.
__________________
klippy is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 07-16-2018 , 11:45   Re: Prevent double looping
Reply With Quote #2

Quote:
Originally Posted by KliPPy View Post
I don't even understand what you mean by "double looping", nested loops are a normal thing.
Having a loop inside another one, basically a nested loop as you said.
__________________

Last edited by edon1337; 07-16-2018 at 11:46.
edon1337 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 07-16-2018 , 12:13   Re: Prevent double looping
Reply With Quote #3

Quote:
Originally Posted by edon1337 View Post
Having a loop inside another one, basically a nested loop as you said.
Yes, and what's the problem with that? You can nest loops as much as you want.
__________________
klippy is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 07-16-2018 , 12:15   Re: Prevent double looping
Reply With Quote #4

Quote:
Originally Posted by KliPPy View Post
Yes, and what's the problem with that? You can nest loops as much as you want.
Quote:
Originally Posted by edon1337 View Post
I think it is bad, because every time the first loop is activated, it activates the 2nd loop, even if it has already finished. And it will not let the 2nd loop continue, it will be stuck.

I had this problem once that's why I'm avoiding double looping
Here's the time I used a nested loop and had problems https://forums.alliedmods.net/showpo...9&postcount=19
__________________

Last edited by edon1337; 07-16-2018 at 12:35.
edon1337 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 07-16-2018 , 13:01   Re: Prevent double looping
Reply With Quote #5

Quote:
Originally Posted by edon1337 View Post
Here's the time I used a nested loop and had problems https://forums.alliedmods.net/showpo...9&postcount=19
The problem is not from nested loops, the problem is that the logic you are using is wrong.
If you write bad code it won't work, don't blame loops for that, they do what you tell them to do.

In your example:
PHP Code:
for( new isizeof g_szValuesi++ )
    {
        for( new 
iValue=ARM_MP5iValue <= ARM_SMOKEGRENADEiValue++ )
        {
            
TrieSetCellg_tWeaponTrieg_szValues], iValue );
        }
    } 
What you are doing is this:
Code:
i = 0 g_szValues = ARM_MP5
TrieSetCell(g_TWeaponTrie, ARM_MP5, 0)
TrieSetCell(g_TWeaponTrie, ARM_MP5, 1)
...
TrieSetCell(g_TWeaponTrie, ARM_MP5, 18)

i = 1 g_szValue = ARM_TMP
TrieSetCell(g_TWeaponTrie, ARM_TMP, 0)
TrieSetCell(g_TWeaponTrie, ARM_TMP, 1)
...
TrieSetCell(g_TWeaponTrie, ARM_TMP, 18)

.
.
.

i = 18 g_szValue = ARM_SMOKEGRENADE
TrieSetCell(g_TWeaponTrie, ARM_SMOKEGRENADE, 0)
TrieSetCell(g_TWeaponTrie, ARM_SMOKEGRENADE, 1)
...
TrieSetCell(g_TWeaponTrie, ARM_SMOKEGRENADE, 18)
For each ARM_* you push 19 times into trie with different values and same key. A trie can't have the same key multiple times, so each push with the same key will overwrite the previous one.

I guess you are trying to match ARM_* to it's value, right? Then all you would have to do is:
PHP Code:
for( new isizeof g_szValuesi++ )
{
          
TrieSetCellg_tWeaponTrieg_szValues], i);

From what I can see you really need to slow down things and ask yourself "what am I trying to do" or "how is my code actually working"? Take some paper and manually execute your code. If you did that you would have noticed the issue that I pointed before.
Of course you could have used debug messages, but that may not be enough for now. Using paper allows you to think at your own pace and see exactly what's happening, step by step.
__________________

Last edited by HamletEagle; 07-16-2018 at 13:04.
HamletEagle is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 07-16-2018 , 13:23   Re: Prevent double looping
Reply With Quote #6

Quote:
Originally Posted by HamletEagle View Post
The problem is not from nested loops, the problem is that the logic you are using is wrong.
If you write bad code it won't work, don't blame loops for that, they do what you tell them to do.

In your example:
PHP Code:
for( new isizeof g_szValuesi++ )
    {
        for( new 
iValue=ARM_MP5iValue <= ARM_SMOKEGRENADEiValue++ )
        {
            
TrieSetCellg_tWeaponTrieg_szValues], iValue );
        }
    } 
What you are doing is this:
Code:
i = 0 g_szValues = ARM_MP5
TrieSetCell(g_TWeaponTrie, ARM_MP5, 0)
TrieSetCell(g_TWeaponTrie, ARM_MP5, 1)
...
TrieSetCell(g_TWeaponTrie, ARM_MP5, 18)

i = 1 g_szValue = ARM_TMP
TrieSetCell(g_TWeaponTrie, ARM_TMP, 0)
TrieSetCell(g_TWeaponTrie, ARM_TMP, 1)
...
TrieSetCell(g_TWeaponTrie, ARM_TMP, 18)

.
.
.

i = 18 g_szValue = ARM_SMOKEGRENADE
TrieSetCell(g_TWeaponTrie, ARM_SMOKEGRENADE, 0)
TrieSetCell(g_TWeaponTrie, ARM_SMOKEGRENADE, 1)
...
TrieSetCell(g_TWeaponTrie, ARM_SMOKEGRENADE, 18)
For each ARM_* you push 19 times into trie with different values and same key. A trie can't have the same key multiple times, so each push with the same key will overwrite the previous one.

I guess you are trying to match ARM_* to it's value, right? Then all you would have to do is:
PHP Code:
for( new isizeof g_szValuesi++ )
{
          
TrieSetCellg_tWeaponTrieg_szValues], i);

From what I can see you really need to slow down things and ask yourself "what am I trying to do" or "how is my code actually working"? Take some paper and manually execute your code. If you did that you would have noticed the issue that I pointed before.
Of course you could have used debug messages, but that may not be enough for now. Using paper allows you to think at your own pace and see exactly what's happening, step by step.
Sorry, I could have gotten the answer from your post back then, but thanks again for the explanation.
__________________
edon1337 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:28.


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