Go Back   EQEmulator Home > EQEmulator Forums > Quests > Quests::Q&A

Quests::Q&A This is the quest support section

Reply
 
Thread Tools Display Modes
  #1  
Old 08-05-2010, 07:03 AM
nenelan
Hill Giant
 
Join Date: Feb 2008
Posts: 116
Default Accessing an Array stored in a Hash, having difficulties

I am having a beast of a time with this. Hopefully someone can help me.

I have an array of strings:
Code:
my @CategoryNames = (Armor, Augments, Weapons, Offslots, Misc);
I also have a Hash for each one of those Categories, along the following lines
Code:
	my %Weapons = 
	(
		alpha =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
		beta =>    [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
		gamma =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
		delta =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
	)
(All values the same right now for testing purposes)

And finally I have a Hash for Levels:
Code:
	my %LevelList =
	(
		65 => delta,
		50 => gamma,
		30 => beta,
		 0 => alpha,
	);
I am trying to make a way to grab the player's level through LevelList, grab a random name from CategoryNames, and then grab a random item through $CategoryName{$LevelCategory}, with $LevelCategory being of alpha, beta, gamma, or delta.

I am able to grab a random element out of CategoryNames fine using:
Code:
$ItemType = $CategoryNames [rand @CategoryNames];
I am able to reference the player's level to get what Level they would fall under in LevelList fine.
Code:
	foreach $item (reverse sort keys %LevelList) {

		if ($MLevel >= $item) {
			$LevelCategory = $LevelList{$item};

		}

	}
(I know, there are easier, more efficient ways to do it, but I'm not worried on that just yet)

I am able to pull a single item out of any one of the arrays stored into Weapons fine using:
Code:
	$Weapons{$LevelCategory}[0];
However, I can not for the life of me use anything in any way similar to:
Code:
	my $Item = $ItemType{$LevelCategory} [rand @ItemType{$LevelCategory}];
I have a feeling it is something stupid and simple that I am missing, but, I can't for the life of me figure it out. I've tried various things and I'm ready to tear my hair out.

Thank you for the help, and please excuse my shoddy Perl coding, still new at this!
Reply With Quote
  #2  
Old 08-05-2010, 07:34 AM
trevius's Avatar
trevius
Developer
 
Join Date: Aug 2006
Location: USA
Posts: 5,946
Default

From your example, I am not sure where you are getting the ItemType hash of arrays from. Should it not be this?:

Code:
my $Item = $Weapons{$LevelCategory} [rand @Weapons{$LevelCategory}];
On another note, your post is probably a nice example of how to make use of arrays, hashes, and hashes of arrays lol.
__________________
Trevazar/Trevius Owner of: Storm Haven
Everquest Emulator FAQ (Frequently Asked Questions) - Read It!
Reply With Quote
  #3  
Old 08-05-2010, 10:22 AM
Tabasco's Avatar
Tabasco
Discordant
 
Join Date: Sep 2009
Posts: 269
Default

References in perl get kind of tricky, and when you ask for a nested list object, you're getting a reference.

Try something like this:
(My examples are just command line demos so I can test outside of emu)
Code:
#!/usr/bin/perl

my @CategoryNames = (Armor, Augments, Weapons, Offslots, Misc);

%Weapons = (
        "alpha" =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
        "beta" =>    [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
        "gamma" =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
        "delta" =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
);

my %LevelList = (
        65 => "delta",
        50 => "gamma",
        30 => "beta",
         0 => "alpha",
    );
#my $ItemType = $CategoryNames [rand @CategoryNames];
my $ItemType = $CategoryNames [2];
my $LevelCategory = "delta";

my $ItemHash = \%$ItemType;
my $Item = ${$ItemHash}{$LevelCategory} [rand @{ ${$ItemHash}{$LevelCategory} }];

print "$Item\n";
Or you could do this:

Code:
#!/usr/bin/perl

my @CategoryNames = (Armor, Augments, Weapons, Offslots, Misc);

my %Categories = (
    "Armor" => {
        "alpha" =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
        "beta" =>    [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
        "gamma" =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
        "delta" =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
    },
    "Augments" => {
        "alpha" =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
        "beta" =>    [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
        "gamma" =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
        "delta" =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
    },
    "Weapons" => {
        "alpha" =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
        "beta" =>    [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
        "gamma" =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
        "delta" =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
    },
    "Offslots" => {
        "alpha" =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
        "beta" =>    [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
        "gamma" =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
        "delta" =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
    },
    "Misc" => {
        "alpha" =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
        "beta" =>    [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
        "gamma" =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
        "delta" =>   [50524,50525,50526,50527,50527,50529,50530,50531,50532,50533,50534,50535],
    }
);

my %LevelList = (
        65 => "delta",
        50 => "gamma",
        30 => "beta",
         0 => "alpha",
    );

my $LevelCategory = "delta";

my $ItemPool = $Categories{$CategoryNames[rand @CategoryNames]}{$LevelCategory};
my $Item = ${$ItemPool} [rand @{$ItemPool}];
print "$Item\n";
Note ${$reference} or @{$reference} syntax.
Reply With Quote
  #4  
Old 08-05-2010, 07:37 PM
nenelan
Hill Giant
 
Join Date: Feb 2008
Posts: 116
Default

Perfect, Tabasco, thanks. I originally tried a set up similar to the second one, but did not know you could nest an array in a hash in a hash. But now that I think of it, why wouldn't it? Learned something new with that reference deal. I'll have to read up on it.

Many thanks!
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

   

All times are GMT -4. The time now is 09:45 PM.


 

Everquest is a registered trademark of Daybreak Game Company LLC.
EQEmulator is not associated or affiliated in any way with Daybreak Game Company LLC.
Except where otherwise noted, this site is licensed under a Creative Commons License.
       
Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Template by Bluepearl Design and vBulletin Templates - Ver3.3