SA-MP Forums Archive
How to read from array - 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: How to read from array (/showthread.php?tid=350971)



How to read from array - ikey07 - 14.06.2012

Alright so, I need small advice how to read from this array

new Fruits[][512] = {
{"Fruits1","Tomato","Apple","Citron"},
{"Fruits2","Cherry","Mango","Orange"}
};


printf("%s are: %s %s %s",Fruits[0],....

printf("%s are: %s %s %s",Fruits[1],....


so how to read from Fruits array?


Re: How to read from array - JaTochNietDan - 14.06.2012

You've pretty much guessed it right. You specify the cell in the 1st dimension to get the contents of it, for example:

pawn Код:
printf("Fruits are: %s, %s, %s, %s", Fruits[0], Fruits[1], Fruits[2], Fruits[3]);
// Fruits are: Fruits 1, Tomato, Apple, Citron
Hope that helps!


Re: How to read from array - ikey07 - 16.06.2012

But it will print Fruits1 and then only 1st chars from the rest

in console will be " Fruits1 are T A C " as its prints single character of Fruits[2],Fruits[3],..


Re: How to read from array - iggy1 - 16.06.2012

How do you want to read it? When you do Fruits[0] do you want to see "Fruits1 Tomato Apple Citron"?

If so your array should be like this.

pawn Код:
new Fruits[][] =
{
    {"Fruits1 Tomato Apple Citron"},
    {"Fruits2 Cherry Mango Orange"}
};



Re: How to read from array - ikey07 - 16.06.2012

Fruits I took as example, I want to pick random RP question.


like

new Fruits[][512] = {
{"Fruits1","Tomato","Apple","Citron"},
{"Fruits2","Cherry","Mango","Orange"}
};

new r = random(sizeof(Fruits));

SendClientMessage(playerid,COLOR,Fruits[r][0]);//Question
SendClientMessage(playerid,COLOR,Fruits[r][1]);//Answer A
SendClientMessage(playerid,COLOR,Fruits[r][2]);//Answer B
SendClientMessage(playerid,COLOR,Fruits[r][3]);//Answer C

But only Fruits[r][0] works as need, rest its print as signle char


Re: How to read from array - iggy1 - 16.06.2012

Use it like this.
pawn Код:
SendClientMessage(playerid,COLOR,Fruits[r]);//
Don't add second dimension if you index the second dimension you are getting a char, if you don't use the second dimension all of the string will be used.


Re: How to read from array - ikey07 - 16.06.2012

Still doesnt help as I want that its also picks 3 answers of each question