Ternary operator in a foreach loop
#1

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
   );
}
Reply
#2

Is it giving errors / warning? Say something..

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

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
Reply
#4

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

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.
Reply
#6

That is fucked you are not even iterating anything.
Reply
#7

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.
Reply
#8

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) { }
Reply
#9

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.
Reply
#10

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

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.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)