Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

returning multiple arrays of hashes from routine for HTML template 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hello , I've having a mare trying to return multiple arrays of hashes from a sub routine into usable arrays for the HTML Template module.
Code:
sub init_period {

my @chase = &getSQL("table","colums","where","order"); 

my @init= &getSQL("table","colums","where","order"); 

return (\@init, \@chase);
}
I'm trying to dereference them before use with the following...
Code:
# Check for initial period records
my ($init,$chase) = &init_period($ADMIN,$STAFF,$user);
my @init = @$init;
my @chase = @$chase;

if(@init > 0){
    $template->param( 'init' => \@init );
}
if(@chase > 0){
    $template->param( 'chase' => \@chase );
}

but the template module is erroring with..
HTML::Template::param() : attempt to set parameter 'init' with an array ref - parameter is not a TMPL_LOOP!

But if I print either @init or @$init, it prints
HASH(0x2c1b418)

is it possible to return multiple arrays from a sub and use in the manner i wish ?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
I found the answer
Code:
# Check for initial period records
my ($init,$chase) = &init_period($ADMIN,$STAFF,$user);

if(@$init > 0){
    $template->param( 'init' => \@$init );
}

if(@$chase > 0){
    $template->param( 'chase' => \@$chase );
}

It seems as though you pass the refrence of the aray back to scalars, and then reference it with the backslash (\) while dereferencing the scalar with the @$.

have I got that right?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Looks like you're passing them properly. To me, it seems the problem is with your template. You have a tmpl_var called 'init', but since you're passing an array to it, it's expecting it to be a tmpl_loop instead.
 
no before I started referencing / dereferencing them properly , they were showing as hashes - well wierd?

I was definately trying to loop passing in an array ref, well I thought I was, it's working now i've got the referencing / dereferencing right, even if i don't quite understand it - lol - what's new.

:)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Whenever I'm having trouble with dereferencing, I find it helps to use Data::Dumper to print the thing I'm trying to process. It helps me to visualise the structure.
Perl:
use Data::Dumper;

my $ref = &init_period($ADMIN,$STAFF,$user);

print Dumper($ref);

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
As $init and $chase are references to arrays, this should work (and would be the correct way to do it):
$template->param( 'init' => $init );

so I don't need to use \@$init ?

But is till need to use @$init > 0 I take it to get number of array indexes right?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
spot on prex1 ,

duh, well it was silly o'clock last night / this morning!

of course seings as $init/$chase are already scalars holding array refs, simply pass the scalar, it just didn't look right when you are used to using \@array for the template module.





"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top