PDA

View Full Version : Need help with procedures and arrays


Mooncalf
02-18-2004, 01:07 AM
Hey y'all,

Once again I've run head-first into the wall of ignorance. I don't know as much about procedures as I think I do. :)

Here's the deal: what do I have to do to make a string available/recognizable inside a procedure?

That is:


string $food[] = {"Apple","Orange","Lemon"};

for ($fruit in $food)
{
print ($fruit);
}


works fine. But:


string $food[] = {"Apple","Orange","Lemon"};

proc foodProc()
{
for ($fruit in $food)
{
print ($fruit);
}
}


returns two "undeclared variables" errors... one for $fruit and one for $food.

I've tried placing the string inside the procedure's parenthesis in many different ways, but I can't get that to work, either. I figure I have the syntax wrong, or it doesn't work the way I'm comprehending it.

Can anyone shine their MEL flashlight my way?

- M

macaroniKazoo
02-18-2004, 02:04 AM
it depends what you're trying to do here... do you want to pass food as a variable? or is it a global variable, accessible from inside the proc?

string $food[] = {"Apple","Orange","Lemon"};

global proc foodProc( string $food[] ){
for ($fruit in $food) print ($fruit + "\n");
}calling: foodProc $food
would work with this. Or you could do this as well:

global string $food[] = {"Apple","Orange","Lemon"};

global proc foodProc(){
global string $food;
for ($fruit in $food) print ($fruit + "\n");
}either will work, it just depends on what you're doing. of course, as always, its best to steer clear of global variables whenever possible.

BTW, its quite acceptible to just say:
print $food;

printing arrays just prints each element on a new line.

Mooncalf
02-18-2004, 02:21 AM
Hey Macaroni,

Hm... I didn't know that print $food; would work. Thanks for the tip! :)

However, what I'm trying to do actually has nothing to do with printing or with food. But I tried to boil down my question to be as easy to ask (and answer) as possible. And I think you may have given me some clues as to where I'm going wrong.

The actual thing I'm working on has some eval stuff going on, and some big-all strings of text mixed in with variables from the array. It's all very complicated and way over my head, but I'm trying to convince myself that it's not. ;)

I'll post here if I have any more trouble. Thanks again!


- M

macaroniKazoo
02-18-2004, 02:24 AM
yeah i did have a feeling that your example was bogus. ;)

yeah, getting eval strings right is often a pain in the ass. on the plus side though, if you simply replace the command eval, with print, you can actually see the command thats being run. I always do a print when i'm creating complicated eval strings to run untill i'm sure the command string it correct, then just replace the print with an eval.

anyway, best'o'luck. :thumbsup:

Mooncalf
02-18-2004, 02:30 AM
Wow... that's exactly what I've been doing! (the whole print/eval thing)

I'll try to share what I've got going on once I'm finished (enough) with it. It's not much, but it's fun and helps me learn.

- M

CGTalk Moderation
01-17-2006, 11:00 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.