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 = ( Code:
$client->Message(315, "At this time, I am able to convert a $cost_link along Code:
my $gaveitem = 0; 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. :) |
If I am reading you right, I think you might be looking for $itemcount
|
Sorry, I guess I wasn't totally clear. I need a way to have perl tell me how many entries are in my $turnins array I create at the beginning. That way I don't need to manually create another variable for that number myself. (4 in this case, since I have 4 item + reward entries in my example array) In PHP I could use the count() function to do this:
Code:
$max_turnins = count($turnins); Thanks though |
couldn't you use the ids of the turn-ins as keys to a hash that has the fabled item-id and costs in another hash? Then you wouldn't need an integer index, or a loop.
|
Not a lot of experience with perl either, but you just want to know the length of the array? A quick google got me this blog posting with 3 ways to determine the number of elements in an array: http://www.devdaily.com/blog/post/pe...gth-perl-array .
Code:
A frequently asked question Perl question is "How do I determine the size of a Perl array?", or the equivalent "How do I determine how many elements are in a Perl array?" |
Those are promising and exactly what I want; unfortunately, either it doesn't work for multidimensional arrays, or the way perl and the emulator interact doesn't allow that syntax. I had previously tried the $#array method before, but additionally tried the other two just now and no go. :(
|
Do the following work?
Code:
my %turnins = ( Code:
$client->Message(315, "At this time, I am able to convert a $cost_link along Code:
my $gaveitem = 0; |
aargh typo:
Code:
|
Thank you for that! You got me exactly where I wanted to get to, though I took a slightly different approach for the array.
Using the quest turnin item ID as the primary index in my array was just the ticket. I'll put up the whole quest in another post, but here's the interesting snippets of the finished product: Array creation: Code:
my %fabled_turnins = ( Code:
foreach $item_id (sort keys %fabled_turnins) |
All times are GMT -4. The time now is 10:58 PM. |
Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.