|
%cat hash_of_array.pl
#!/usr/bin/perl -U
#==============================================================================#
package main;
#==============================================================================#
local $items = new JMJSITEM ("fruit","car");
$items->{"fruit"}->add("apple","banana","tomato");
$items->{"car"}->add("Hyundai","Daewoo","Kia");
print "items:";
$items->print_item();
exit 0;
#==============================================================================#
package JMJSITEM;
#==============================================================================#
#------------------------------------------------------------------------------#
sub new {
#------------------------------------------------------------------------------#
my ($class, @data) = @_;
my $ref = {};
bless $ref, $class;
while(@data) {
$jmjsitem = new JMJSLIST;
%$ref=(%$ref,$data[0],$jmjsitem);
shift(@data);
}
return $ref;
}
#------------------------------------------------------------------------------#
sub print_item {
#------------------------------------------------------------------------------#
my ($ref) = @_;
print "${ref}\>\n";
foreach $key (keys %$ref) {
print " $key:";
$ref->{$key}->print_list(" ");
}
}
#==============================================================================#
package JMJSLIST;
#==============================================================================#
#------------------------------------------------------------------------------#
sub new {
#------------------------------------------------------------------------------#
my ($class, @data) = @_;
my $ref = [];
bless $ref, $class;
if(@data) {$ref->add(@data);}
return $ref;
}
#------------------------------------------------------------------------------#
sub add {
#------------------------------------------------------------------------------#
my ($ref, @data) = @_;
@$ref=(@$ref,@data);
}
#------------------------------------------------------------------------------#
sub print_list {
#------------------------------------------------------------------------------#
my ($ref,$tab) = @_;
print "$ref\>\n$tab=(";
print join(",\n$tab",@$ref);
print ")\n";
return 1;
}
exit 0;
%perl hash_of_array.pl
items:JMJSITEM=HASH(0x77d064)>
fruit:JMJSLIST=ARRAY(0x77d094)>
=(apple,
banana,
tomato)
car:JMJSLIST=ARRAY(0x770b20)>
=(Hyundai,
Daewoo,
Kia)
%
|
|