Question about Arrays

I was wondering if there was an easy way to put numbers into an $ints[] array.

For instance, I can do something like
Code:
foreach it in $ints[5, 2, 9, 4, 22, 199]
//functions and such
but I put those there by hand.

Is there a way to put values calculated from functions into an $ints[] array? Or is that basically what maps are for?
 

Veracity

Developer
Staff member
$ints[] is a constant. An array is, essentially, a fixed-size map indexed by an int. You can declare your own like this:

Code:
string [10 ] myarray;

myarray[0] = "hello";
myarray[7] = "sailor";

foreach index, value in myarray {
    if ( value != "" )
	print( "myarray[" + index + "] = " + value );
}
which yields:

> array.ash

myarray[0] = hello
myarray[7] = sailor
 
Thanks Veracity, that is kind of what I figured.

On a different note, can I sort a map based on what of its record values? For instance....
Code:
record my_rec {
int numbers;
int values;
};
my_rec [int]map;
//fill the record ints with numbers
//want to sort by the "numbers" rec based on value

Not sure how helpful that example was...lol

Another random question, is there a way to have variable defining maps? As in the map being defined varies on a number. For example....
Code:
int[int] = map1, map2, map3;
for x from 1 to 3 {
    map+x+[5]=10;
}

I've tried a few random methods but none seemed to work =\
 
Last edited:

Bale

Minion
On a different note, can I sort a map based on what of its record values? For instance....
Code:
record my_rec {
int numbers;
int values;
};
my_rec [int]map;
//fill the record ints with numbers
//want to sort by the "numbers" rec based on value

Not sure how helpful that example was...lol

Sure. You know the sort command, right? If you use the "value" keyword, you can choose the proper record element as if value was the name of the record.

Code:
sort map by value.values;

Another random question, is there a way to have variable defining maps? As in the map being defined varies on a number. For example....
Code:
int[int] = map1, map2, map3;
for x from 1 to 3 {
    map+x+[5]=10;
}

I've tried a few random methods but none seemed to work =\

Don't think of them as separate maps, but as an additional dimension in a single map. I'm reasonably sure that is what you actually are doing, so it shouldn't be hard.

Code:
int [int][int] map;
for x from 1 to 3
   map[x][5] = 10;
 
Last edited:
Sure. You know the sort command, right? If you use the "value" keyword, you can choose the proper record element as if value was the name of the record.

Code:
sort map by value.values;

Thanks Bale! That is exactly what I was looking for.


Don't think of them as separate maps, but as an additional dimension in a single map. I'm reasonably sure that is what you actually are doing, so it shouldn't be hard.

Code:
int [int][int] map;
for x from 1 to 3
   map[x][5] = 10;

Well, I could have used a multi-deminsional map for the problem, but when I use the count() function it would return numbers that didn't really make sense for the problem. So I was trying to keep them one dimensional so I could count them easily to get a simple integer of the amount of keys it held.


It is entirely probable that I was doing something strange/wrong with the function tho.
 

Bale

Minion
Well, I could have used a multi-deminsional map for the problem, but when I use the count() function it would return numbers that didn't really make sense for the problem. So I was trying to keep them one dimensional so I could count them easily to get a simple integer of the amount of keys it held.

Not a problem: count(map[1]) works just fine.
 
Top