Gossamer Forum
Home : General : Perl Programming :

two arrays into one hash

Quote Reply
two arrays into one hash
@class = (
'bad', 'old', 'good', 'super', 'elemental',
'unknown', 'superior', 'insane', 'powerfull', 'heavenly'
);

@items = (
'shoe', 'belt', 'gloves', 'bracers', 'armor',
'sword', 'lance', 'hammer', 'helmet', 'ring'
);

# Normally I do this if its few item oke but if a 1000 then the file became a bit to large
$c=0;$n=0;
%hash = ( #[num, minvalue, maxvalue, magicvalue]
"$class[$c] $items[$n]" =>[0,0,10,0,''],
"$class[$c++] $items[$n]" =>[1,1,11,1,''],
"$class[$c++] $items[$n]" =>[2,2,12,2,''],
"$class[$c++] $items[$n]" =>[3,3,13,3,''],
"$class[$c++] $items[$n]" =>[4,4,14,4,''],
"$class[$c++] $items[$n]" =>[5,5,15,5,''],
"$class[$c++] $items[$n]" =>[6,6,16,6,''],
"$class[$c++] $items[$n]" =>[7,7,17,7,''],
"$class[$c++] $items[$n]" =>[8,8,18,8,''],
"$class[$c++] $items[$n]" =>[9,9,19,9,''],
"$class[$c=0] $items[$n++]" =>[10,10,20,10,''],
"$class[$c++] $items[$n]" =>[11,11,21,11,''],
"$class[$c++] $items[$n]" =>[12,12,22,12,''],
"$class[$c++] $items[$n]" =>[13,13,23,13,''],
"$class[$c++] $items[$n]" =>[14,14,24,14,''],
"$class[$c++] $items[$n]" =>[15,15,25,15,''],
"$class[$c++] $items[$n]" =>[16,16,26,16,''],
"$class[$c++] $items[$n]" =>[17,17,27,17,''],
"$class[$c++] $items[$n]" =>[18,18,28,18,''],
"$class[$c++] $items[$n]" =>[19,19,29,19,''],
# and so on
);
# There must an other way to do this I think I've been puzzling for a
# few hours now and can't figure it out how to do this.
# If someone can solve this problem for me I would be greatfull.
# Mail solutions to jim@freearena.com or ICQ# 106869497 thank you.

Freearena Entertainment
Quote Reply
Re: two arrays into one hash In reply to
if you take an object oriented approach to your code you will be able to shrink it exponentially.
since you already have arrays of your classes and items, use the number of the position in that array to reference the item or class rather than a string of their values.

also, it looks like you are creating a lot of data that you could build dynamically when you need to access it rather than create a huge hash that may have elements that never get used.

imagine a system something like this:

Code:
class Item;
class Weapon;
class EQ;
class Food;
...

class Player;
class NPC;
class PC;

class Room;
...
you will save yourself a ton of time and speed using an OO approach.

as i said, try to use normalization as much as possible.
that is, figure out ways to represent your data through reference. for instance, have an array of item conditions and an array of actual items. then create instances of each item by the item and condition ids.

something like:
Code:
my @itemCon = qw(
Shabby
Poor
Average
Solid
Lethal
);

my @items = (
# name dmg hitDice cost
['Dagger', 6, 2, 10],
['Staff', 4, 1, 5],
...
);

then, a player's inventory could be:

[0, 2], # an average dagger
[1, 4], # a lethal staff
etc

-g


s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';