help with FOR syntax

Camber

Member
Hi -

I need some syntax help with a FOR or FOREACH statement. I want to reiterate a few lines over several variables that are not numbers (or items, etc.). But i can't seem to get the correct combination of syntax. From the wiki, it looks like a foreach will work, but it errors out on "Aggregate reference expected", and FOR seems to expect a series.

Can anyone point me in the correct direction? Thanks!

PHP:
string var1 = "cat" ; 
string var2 = "3dog" ; 
string var3 = "bird-232" ;
foreach x in (var1, var2, var3) {
	print(x) ;
}
 

Camber

Member
foreach works exclusively with maps.

Code:
foreach x in $strings[cat, 3dog, bird-232]

Ok, I see that now, but i don't think that will help me in my script. Basically, i am writing a script to find any variations of the squish/foldable items of the month and print them out. I am passing in the n, o1, o2, o3, etc. in the following code.

If I can't use variables, is there a different looping structure I can use? I have the code working with a series of if statements, but that is cumbersome to make changes.

PHP:
int folds( item n, item o1, item o2, item o3, item o4, item o5, item o6, string m ) {
[snip other code]

foreach itm in $items[n, o1, o2, o3, o4, o5, o6] {
  if(item_amount(itm) != 0 ) {
    gp = getPrice(itm) ;
    print_html("Inventory: " + item_amount(itm) + " " + itm + " " + rnum( gp ) ) ;
    tprice = tprice + (item_amount(itm) * gp) ;
  }
}
 

Bale

Minion
What about something like...

Code:
record foldable {
   item [int] forms
};

foldable [int] folds

foreach x in folds
   foreach itm in folds[x]
      iif(item_amount(itm) > 0 ) {
...
 
Last edited:

icon315

Member
Well this is what i came up with, not sure if it is what you wanted

PHP:
void Mall(int i){
item itm = i.to_item();
  if(item_amount(itm) != 0 ) {
    int gp = mall_price(itm) ;
    print("Inventory: " + item_amount(itm) + " " + itm + " Priced at " + gp  ) ;
    int tprice = item_amount(itm) * gp ;
	print("Total: "+tprice);
  }

}


for i from 1423 to 1427 {
	Mall(i);

} 

for i from 2221 to 2226 {
	Mall(i);

} 

for i from 3192 to 3197 {
	Mall(i);

} 

for i from 3661 to 3667 {
	Mall(i);

} 

for i from 4398 to 4403 {
	Mall(i);

}
 

Bale

Minion
I like Icon315's solution. For some reason I always forget about contiguous item numbers.

Though I'm going to point out that tprice isn't doing what Camber wants. It's a small fix however.
 
Top