PDA

View Full Version : Accessing an Array stored in a Hash, having difficulties


nenelan
08-05-2010, 07:03 AM
I am having a beast of a time with this. Hopefully someone can help me.

I have an array of strings:
my @CategoryNames = (Armor, Augments, Weapons, Offslots, Misc);

I also have a Hash for each one of those Categories, along the following lines
my %Weapons =
(
alpha => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
beta => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
gamma => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
delta => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
)

(All values the same right now for testing purposes)

And finally I have a Hash for Levels:
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:
$ItemType = $CategoryNames [rand @CategoryNames];

I am able to reference the player's level to get what Level they would fall under in LevelList fine.
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:
$Weapons{$LevelCategory}[0];

However, I can not for the life of me use anything in any way similar to:
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! :)

trevius
08-05-2010, 07:34 AM
From your example, I am not sure where you are getting the ItemType hash of arrays from. Should it not be this?:

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.

Tabasco
08-05-2010, 10:22 AM
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)

#!/usr/bin/perl

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

%Weapons = (
"alpha" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
"beta" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
"gamma" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
"delta" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,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:


#!/usr/bin/perl

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

my %Categories = (
"Armor" => {
"alpha" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
"beta" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
"gamma" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
"delta" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
},
"Augments" => {
"alpha" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
"beta" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
"gamma" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
"delta" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
},
"Weapons" => {
"alpha" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
"beta" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
"gamma" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
"delta" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
},
"Offslots" => {
"alpha" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
"beta" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
"gamma" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
"delta" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
},
"Misc" => {
"alpha" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
"beta" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
"gamma" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,50533,50534,50535],
"delta" => [50524,50525,50526,50527,50527,50529,50530,50531,50 532,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.

nenelan
08-05-2010, 07:37 PM
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! :)