PDA

View Full Version : placing Array of Array references in hash


tom123
08-01-2006, 12:13 PM
Problem trying to assign values of an array of array references (@product_records) to a hash based on colum names (@headers).


sub prepare_data_for_output() {

my $reference = shift;
my (@product_records) = @{$reference};
my $reference2 = shift;
my (@headers) = @{$reference2};

my @products_ref = ();

foreach my $row(@product_records)
{
my $h=0;
foreach my $field($product_records[$row])
{
my %products;
$products{$headers[$h]} = $field;
$h++;
}
push(@products_ref,\%products);
}

return (@products_ref);
}


(@products_ref) = &prepare_data_for_output(\@product_records, \@colums);

# Begin page output using template
print CGI::header();
# my $template = HTML::Template->new( filename => '../templates/admin_products.tmpl' );
$template->param(PRODUCTS => \@products_ref);
print $template->output();

Could someone please tell me why values are not being assigned to hash corectly.

Thanks in advance

tom123
08-01-2006, 12:44 PM
Fixed it guys
sorry

tom123
08-01-2006, 12:46 PM
sub prepare_data_for_output() {

my $reference = shift;
my (@product_records) = @{$reference};
my $reference2 = shift;
my (@headers) = @{$reference2};

my @products_ref = ();

foreach my $row(@product_records)
{
my %products;
my $h=0;
foreach my $field(@{$row})
{
$products{$headers[$h]} = $field;
$h++;
}
push(@products_ref,\%products);
}

return (@products_ref);
}

KevinADC
08-02-2006, 08:07 PM
this line:

sub prepare_data_for_output() {

is probably better written as:

sub prepare_data_for_output {

when you put the parenthesis on the end of a sub routine name you create a prototype in perl. Prototypes work a little bit differently than regular sub routines and if you are not aware of the differences it can be very frustrating trying to figure out why the prototype is not behaving like you expect it to.