Raised This Month: $32 Target: $400
 8% 

Solved Prevent double looping


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

I still don't understand what you mean by "even if it has already finished" and "it will be stuck". Your problem still makes no sense to me.
__________________
klippy 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 #12

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 #13

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
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 07-16-2018 , 13:46   Re: Prevent double looping
Reply With Quote #14

I wanted to point that using nested loops it's not a problem, in fact it's pretty common practice.

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

BUT, I realized I don't need the 2nd loop so I'm okay for now.
And about this, as Klippy said, I am not sure to understand what's the problem. Are you saying that the first loop will make the second loop activate again?
If that's what you are saying, then it should be obvious, that's the entire purpose of a loop, to execute the code from it's body, no matter what's code is in there.

For example this:
PHP Code:
for(1<= 5i++)
{
      for(
1<= 3j++)
      {
           
server_print("%d+ %d = %d"ij i+j)
      }

Will execute like this:
Code:
i = 1
   j = 1 prints 1 + 1 = 2
   j = 2 prints 1 + 2 = 3
   j = 3 prints 1 + 3 = 4
i = 2
   j = 1 prints 2 + 1 = 3
   j = 2 prints 2 + 2 = 4
   j = 3 prints 2 + 3 = 5
.
.
.
i = 5
   j = 1 prints 5 + 1 = 6
   j = 2 prints 5 + 2 = 7
   j = 3 prints 5 + 3 = 8
__________________

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

Quote:
Originally Posted by HamletEagle View Post
I wanted to point that using nested loops it's not a problem, in fact it's pretty common practice.



And about this, as Klippy said, I am not sure to understand what's the problem. Are you saying that the first loop will make the second loop activate again?
If that's what you are saying, then it should be obvious, that's the entire purpose of a loop, to execute the code from it's body, no matter what's code is in there.

For example this:
PHP Code:
for(1<= 5i++)
{
      for(
1<= 3j++)
      {
           
server_print("%d+ %d = %d"ij i+j)
      }

Will execute like this:
Code:
i = 1
   j = 1 prints 1 + 1 = 2
   j = 2 prints 1 + 2 = 3
   j = 3 prints 1 + 3 = 4
i = 2
   j = 1 prints 2 + 1 = 3
   j = 2 prints 2 + 2 = 4
   j = 3 prints 2 + 3 = 5
.
.
.
i = 5
   j = 1 prints 5 + 1 = 6
   j = 2 prints 5 + 2 = 7
   j = 3 prints 5 + 3 = 8
I was saying that 1st loop would keep activating 2nd loop until 1st loop would end.
So if 1st loop has to repeat 20 times and 2nd loop 10 times, both loops would repeat 20 times.
__________________
edon1337 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 07-16-2018 , 13:54   Re: Prevent double looping
Reply With Quote #16

Quote:
I was saying that 1st loop would keep activating 2nd loop until 1st loop would end.
So if 1st loop has to repeat 20 times and 2nd loop 10 times, both loops would repeat 20 times.
That's not how it works.
First iteration of first loop: 10 iterations of second loop
Second iteration of first loop: 10 iterations of second loop
Third iteration of first loop: 10 iterations of second loop
.
.
.
20th iteration of first loop: 10 iterations of second loop

What's the problem?
__________________
HamletEagle is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 07-16-2018 , 14:01   Re: Prevent double looping
Reply With Quote #17

Quote:
Originally Posted by HamletEagle View Post
That's not how it works.
First iteration of first loop: 10 iterations of second loop
Second iteration of first loop: 10 iterations of second loop
Third iteration of first loop: 10 iterations of second loop
.
.
.
20th iteration of first loop: 10 iterations of second loop

What's the problem?
Oh, no problem at all, now I get it. Thanks once again for the explanation, I wasn't paying attention on how it works
__________________
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 01:35.


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