PDA

View Full Version : Semi complex data structure, setting hash of array of hash


grapnell
09-18-2006, 08:41 PM
Hi, I am trying to write a function to set an array in my hash, to another hash.
My data structure is as follows, with some parts eliminated for simplification:

package Order;
use Data::Dumper;
require "settings.pl";
$Data::Dumper::Purity = 1;
##########################
# Order Class -- Try One #
##########################

BEGIN {}

sub new
{
my $self = {};
my @items = [];
shift;
$self->{ORDER_NUM} = shift;
$self->{ENTRY_DATE} = shift;
$self->{SHIP_DATE} = shift;
$self->{VENDOR} = shift;
$self->{PO_NUM} = shift;
$self->{CUST_NUM} = shift;
$self->{BILL_ADDR} = shift;
$self->{SHIP_ADDR} = shift;
$self->{CARRIER} = shift;
$self->{FRT_CHG} = shift;
$self->{CLIPBOARD} = shift;
$self->{RUSH} = shift;
$self->{SALESPERSON} = shift;
$self->{ENTERED_BY} = shift;
$self->{ITEMS} = @items;
bless($self);
return $self;
}
sub addProduct
{
my $self = shift;
push ($self->{ITEMS}, {
quan => shift,
len => shift,
wid => shift,
price => shift,
partnum => shift,
desc => shift,
fname => shift,
colors => shift,
type => shift,
border => shift,
ucost => shift,
tcost => shift,
backg => shift,
matyd => shift,
status => shift});
}

sub writeOut
{
my $self = shift;
my $filename = shift;
print $filename;
open (ORDERFILE, ">$filename") or die "can't open $full_order_dir/$ordernum.dat";
print ORDERFILE Data::Dumper->Dump([\$self], ['*self']);
close FILE;
}
return 1;


perl does not recognize $self->{ITEMS} as an array when I call the addProduct function with the following code:

$this_order->addProduct("Q", "L", "W", "P", "PN", "D", "F", "C", "T", "B", "UC", "TC", "BG", "MY", "IS");


and I get this error:
Type of arg 1 to push must be array (not hash element) at ../Order.pl line 196, near "})"

I have been through "Programming Perl" OREILLY books, from cover to cover, but it does not explain how to accomplish this.

what am I doing wrong, and how can I go about fixing it?

Thanks in advance,
Jeff

KevinADC
09-18-2006, 09:29 PM
push (@{$self->{ITEMS}}, {

you might also need to change this line:

my @items = [];

to:

my @items = ();

grapnell
09-18-2006, 10:15 PM
Kevin,

Thanks, that did the trick,
this is my first lesson in OO-Perl though I have done many cgi scripts in the past.

Seems like O'reilly book should go a little more in depth into this subject, or maybe I just overlooked it.

Thanks again,
Jeff