Maniascript array, how to get n:th element out ?

You can talk about ManiaScript for ManiaPlanet here

Moderator: English Moderator

Post Reply
reaby
Posts: 956
Joined: 29 Dec 2010, 23:26
Location: Eastern Finland
Contact:

Maniascript array, how to get n:th element out ?

Post by reaby »

Problem:
I have array with strings as keys... how to access value by index number ?

problematic is when the records array has 500 or 1000 keys... foreach loop comes slow to find the keys (onMouseOver)
User avatar
maxi031
Posts: 378
Joined: 17 Jul 2011, 00:55

Re: Maniascript array, how to get n:th element out ?

Post by maxi031 »

This is the way i would do it:

Code: Select all

declare Text[Integer] myArray = [0=>"value 0", 1=>"value 1", 2=>"value 2"];
than you should be able to access it with number:

Code: Select all

log(myArray[0]);
This should print: value 0;
My specs:
MOBO: MB AM3+ 970 Gigabyte GA-970A-DS3P
CPU: AM3+ AMD FX-8320E
GPU: Nvidia GeForce GTX750-Ti ASUS 2GB DDR5
RAM: DDR3 8GB 1866MHz Kingston HyperX
SSD: SATA3 120gb SanDisk
OS: Ubuntu 19.04
TY MICMO
User avatar
Eole
Nadeo
Nadeo
Posts: 1265
Joined: 26 Apr 2011, 21:08

Re: Maniascript array, how to get n:th element out ?

Post by Eole »

reaby wrote:Problem:
I have array with strings as keys... how to access value by index number ?
You can't. :(

A possible dirty and bug-prone solution if you don't have any alternative would be to the one suggested above by maxi031.

Code: Select all

declare FirstArray = [0 => "A", 1 => "B", 2 => "C"];
declare SecondArray = ["A" => "ZZZ", "B" => "YYY", "C" => "XXX"];
log(SecondArray[FirstArray[2]]); //< XXX
Contribute to the ManiaPlanet documentation on GitHub
A question about ManiaScript? Ask it here!
reaby
Posts: 956
Joined: 29 Dec 2010, 23:26
Location: Eastern Finland
Contact:

Re: Maniascript array, how to get n:th element out ?

Post by reaby »

Eole wrote: A possible dirty and bug-prone solution if you don't have any alternative would be to the one suggested above by maxi031.

Code: Select all

declare FirstArray = [0 => "A", 1 => "B", 2 => "C"];
declare SecondArray = ["A" => "ZZZ", "B" => "YYY", "C" => "XXX"];
log(SecondArray[FirstArray[2]]); //< XXX
This solves my problem totally, as i fear the foreach loop is way too slow.
I can update the first array with foreach easily, and then use the buffered value later.

Many many thanks,
Reaby
Post Reply

Return to “ManiaScript”

Who is online

Users browsing this forum: No registered users and 1 guest