SA-MP Forums Archive
Ternary operator in a foreach loop - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Ternary operator in a foreach loop (/showthread.php?tid=630334)



Ternary operator in a foreach loop - Cypress - 12.03.2017

Would something like this be possible by any similar way instead of having two different loops doing exactly the same thing..

pawn Код:
foreach ( new i : ( pData[ playerid ][ e_PLAYER_SEX ] ? male_appearance : female_appearance ) ) {
   format(
      string,
      sizeof ( string ),
      "%d\n",
      i
   );
}



Re: Ternary operator in a foreach loop - ISmokezU - 12.03.2017

Is it giving errors / warning? Say something..

I don't think foreach could be used in that way, i've never seen it before.


Re: Ternary operator in a foreach loop - Cypress - 12.03.2017

Quote:
Originally Posted by ISmokezU
Посмотреть сообщение
Is it giving errors / warning? Say something..

I don't think foreach could be used in that way, i've never seen it before.
Well it does work in PHP but it's totally two different things.

Код:
error 001: expected token: ";", but found ":"
warning 215: expression has no effect
error 001: expected token: ";", but found ":"
error 029: invalid expression, assumed zero
fatal error 107: too many error messages on one line



Re: Ternary operator in a foreach loop - Unte99 - 12.03.2017

https://sampwiki.blast.hk/wiki/Control_S...res#for_.28.29


Re: Ternary operator in a foreach loop - Cypress - 12.03.2017

Quote:
Originally Posted by Unte99
Посмотреть сообщение
It was all about iterators but never mind. Something similar with a for loop.

pawn Код:
new string[ 40 ];

            for ( new i = 0; i < pData[ playerid ][ e_PLAYER_SEX ] ? sizeof ( male_skins ) : sizeof( female_skins ); i++ ) {
                format(
                    string,
                    sizeof ( string ),
                    "%d\n",
                    pData[ playerid ][ e_PLAYER_SEX ] ? male_skins[ i ] : female_skins[ i ]
                );
            }
Hope it helps anyone who needs something similar.


Re: Ternary operator in a foreach loop - Pottus - 12.03.2017

That is fucked you are not even iterating anything.


Re: Ternary operator in a foreach loop - Cypress - 12.03.2017

Quote:
Originally Posted by Pottus
Посмотреть сообщение
That is fucked you are not even iterating anything.
What's fucked up sir?

The ternary in foreach would of choose between two iterators depending on the gender.


Re: Ternary operator in a foreach loop - Pottus - 12.03.2017

Well you have this bullshit like this.

Код:
foreach ( new i : ( pData[ playerid ][ e_PLAYER_SEX ] ? male_appearance : female_appearance ) ) {
That is not how to use an iterator and makes absolutely no sense.

Код:
new Iterator:ShitIter<1000>;
foreach(new i : ShitIter) { }



Re: Ternary operator in a foreach loop - Cypress - 12.03.2017

Quote:
Originally Posted by Pottus
Посмотреть сообщение
Well you have this bullshit like this.

Код:
foreach ( new i : ( pData[ playerid ][ e_PLAYER_SEX ] ? male_appearance : female_appearance ) ) {
That is not how to use an iterator and makes absolutely no sense.

Код:
new Iterator:ShitIter<1000>;
foreach(new i : ShitIter) { }
Image if you had

Код:
new Iterator:Male<200>,
       Iterator:Female<200>;

foreach(new i : depending on gender, Male | Female ) {
}
What you're talking about is the format of foreach and looping procedure thru an iterator which yes in this case isn't correct. Was just wondering if it'd be possible in any other way instead of having to do foreach twice.


Re: Ternary operator in a foreach loop - Pottus - 12.03.2017

That is not how you use foreach again throw that whole idea out and use an if() statement.


Re: Ternary operator in a foreach loop - Gammix - 13.03.2017

Since foreach is a macro, it might effect the variable names and so one since "define" is just a text replacement.


example:
PHP Code:
#define iter:%1<%2> \
    
%1limit = %2

#define myLoop(%1,%2) \
    
for (new %1; %< %2limit; %1++)

main()
{
    new 
iterTEST<10>;
    
myLoop(iTEST)
    {
    }

    new 
iterTEST2<10>;
    
myLoop(i, (random(2) ? (TEST) : (TEST2))) // this wont work because the macro\'s "%2limit" will become "(random(2) ? (TEST) : (TEST2))limit" which makes no sense
    
{
    }

So an "if" statement is what have to use.