View Single Post
  #1  
Old 10-17-2009, 09:18 PM
Valdaun
Fire Beetle
 
Join Date: Oct 2009
Posts: 28
Question Need some perl array help

I am trying to make a very simple and scalable quest script utilizing arrays of item turnin + rewards. I have it working now, but I am positive I can simplify this just a tad more, hence my post.

Below are some code snippets showing how I am doing it now, then I will ask my questions after the code.

First I define the item turnin + reward array at the top of the .pl file (this particular quest "upgrades" a few old world weapons to fabled status):

Code:
my %turnins = (
	1 => { "item" => 5401, "reward" => 68259 },	#Mithril 2 hander & fabled
	2 => { "item" => 5403, "reward" => 68260 },	#Ghoulbane & fabled
	3 => { "item" => 5500, "reward" => 68263 },	#SS of Ykesha & fabled
	4 => { "item" => 5412, "reward" => 68262 }	#Razing Sword of Skarlon
);

my $max_turnins = 4;	# must manually set to highest number above
my $cost = 22503;	# blue diamond (dropped)
my $cost_link = quest::varlink($cost);
Then in the hailing portion of the script, this is an example of how I use the array and some say + item links:

Code:
$client->Message(315, "At this time, I am able to convert a $cost_link along
with the basic version of any of these weapons into their fabled counterparts:");

# loop through item + reward array and send a list of items to client 
for (my $i = 1; $i <= $max_turnins; $i++)
{
	my $link = quest::varlink($turnins{$i}{"item"});
	$client->Message(315, "$link")
}
Lastly, here is what I am using to actually check for the turnins:

Code:
my $gaveitem = 0;

# loop through item + reward array looking for a match
for (my $i = 1; $i <= $max_turnins; $i++)
{
	# hand out the goods on a match of item + cost
	if (plugin::check_handin(\%itemcount, $turnins{$i}{"item"} => 1, $cost => 1))
	{
		$gaveitem = 1;
		quest::summonitem($turnins{$i}{"reward"});
	}
}

# if there were no matches, give back what was turned in
if ($gaveitem == 0)
{
	plugin::return_items(\%itemcount);
}
Questions -

So the main thing I would like to change is to not have to specify the array index numbers myself, but I do not know enough about perl syntax to know how to do this.

I would then like to know how I can get a count of the number of items in the array. That way I don't have to create the $max_turnins variable.

Lastly, perhaps this would be easier to accomplish using foreach to automatically go through the whole array? So if anyone has an example that could replicate what I am doing above with foreach that would be very helpful.
Reply With Quote