Ask a Question related to PERL Modules, Design and Development.
-
Zaphod #1
Distributing an array of hash refs across a table?
I have a cgi script that polls a SQL database and prints out reports.
some of the reports have more columns then others.
I assume (yes I know about assuming) that the Distributive nature of
CGI.pm's Tr(0 & td() functions are what I need to write a short
subroutine which will table these results
I have tried many permutations of the following subroutine, and can
not figure out how to make this work....
Thanks for any help anyone might offer...
&create_db_connection;
while($Load=&read_load_data_by_number($LoadNumber+ +)){
$aref={};
$Info=&read_info_data_by_id($Load->{'regular'});
$Customer=&read_customer_data_by_id($Load->{'customer'});
$Carrier=&read_carrier_data_by_id($Load->{'carrier'});
$Driver=&read_driver_data_by_id($Load->{'driver'});
$aref->{'pudate'}=$Info->{'pudate'};
$aref->{'number'}=$Load->{'number'};
$aref->{'custname'}=$Customer->{'name'};
$aref->{'carrier'}=$Carrier->{'name'};
$aref->{'gross'}=$Info->{'gross'};
$aref->{'totruck'}=$Info->{'totruck'};
push @LoadList, $aref;
}
&print_list(@LoadList);
&disconnect;
sub print_list{if($DeBug){print 'in print_list, ',$Search,br();}
my @List = @_;
print table({-width=>'100%',-border=>1,-cellspacing=>0,-cellpadding=>6},
Tr({-valign=>'center'},[@List],
td({-align=>'center',-valign=>'top'},{'pudate','number','customer'}),
)
)
}
Zaphod Guest
-
hash of hash of array slices
This works Foreach ( @{$hash{$key1}{$key2}} ) This does note Foreach ( @{($hash{$key1}{$key2})} ) This gives me this error .... Can't... -
adding an array as a hash value
Hi Dermot. Dermot Paikkos wrote: And use warnings; These variables need to have better names so that it's more -
newbie question. how to assign an array to a hash table?
Hi, I have something like: $value = 'A' 'C' 'G'; and I would like store this value on a hash table. my @value = split(' ',$value); print... -
Comparing 2 hashes of array refs
On 11 Jul 2003, simo wrote: It's best to cut and paste from your code, as opposed to retyping. Hashes are defined by parenthesis, not by... -
How to put a filehandle into a hash array?
I need to build an array of filehandles so different records go into different output files depending on a value. This is the code: $dt =... -
Brian McCauley #2
Re: Distributing an array of hash refs across a table?
[email]zaphod@earthling.net[/email] (Zaphod) writes:
Thowing random solutions at a problem is not a good approach. If you> I have a cgi script that polls a SQL database and prints out reports.
> some of the reports have more columns then others.
>
> I assume (yes I know about assuming) that the Distributive nature of
> CGI.pm's Tr(0 & td() functions are what I need to write a short
> subroutine which will table these results
>
> I have tried many permutations of the following subroutine, and can
> not figure out how to make this work....
can't solve your problem consider breaking it into simpler ones.
First, get some good habits.
Always declare all variables in the smallest applicable lexical scope
unless there is a reson to do otherwise. Using "strict" will help you
find variables you've not declared so always work with strict on.
Always enable warnings unless you've decided there's a particular
reason you want to disable a particular warning. If so then disable
the particular warning only and for the smallest appropiate scope.
There is at least one silly typo in your code that would have been
picked up by enabling warnings.
Don't use the & syntax to call subroutines unless you know what it
does and that's really what you want to do.
Indent your code consitantly and readably.
Use slices where appropiate.
Don't make copies of arrays unless you have a reason to do so.
As a general rule, pass array arguments into subroutines as a
reference rather than as a list. (This is a special case of the above).
All these habits will make programming in Perl easier.
If you are interested in distribuiting stuff over lists then> Thanks for any help anyone might offer...
learn about the map function.
I suspect you meant somthing like:> &create_db_connection;
> while($Load=&read_load_data_by_number($LoadNumber+ +)){
> $aref={};
> $Info=&read_info_data_by_id($Load->{'regular'});
> $Customer=&read_customer_data_by_id($Load->{'customer'});
> $Carrier=&read_carrier_data_by_id($Load->{'carrier'});
> $Driver=&read_driver_data_by_id($Load->{'driver'});
> $aref->{'pudate'}=$Info->{'pudate'};
> $aref->{'number'}=$Load->{'number'};
> $aref->{'custname'}=$Customer->{'name'};
> $aref->{'carrier'}=$Carrier->{'name'};
> $aref->{'gross'}=$Info->{'gross'};
> $aref->{'totruck'}=$Info->{'totruck'};
> push @LoadList, $aref;
> }
> &print_list(@LoadList);
> &disconnect;
>
> sub print_list{if($DeBug){print 'in print_list, ',$Search,br();}
> my @List = @_;
> print table({-width=>'100%',-border=>1,-cellspacing=>0,-cellpadding=>6},
> Tr({-valign=>'center'},[@List],
> td({-align=>'center',-valign=>'top'},{'pudate','number','customer'}),
> )
> )
> }
print table({-width=>'100%',-border=>1,-cellspacing=>0,-cellpadding=>6},
Tr({-valign=>'center'},
[ map scalar td(
{-align=>'center',-valign=>'top'},
[@$_{'pudate','number','customer'}]
), @List
]
)
);
This, however, is not very readable. You may want to consider a for
loop and some more variables rather than trying to do it all in one
statement.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
Brian McCauley Guest
-
$_@_.% #3
Re: Distributing an array of hash refs across a table?
> [email]zaphod@earthling.net[/email] (Zaphod) writes:
Hi sorry for butting in... but im new and the posts help me learn.> Always enable warnings unless you've decided there's a particular
> reason you want to disable a particular warning. If so then disable
> the particular warning only and for the smallest appropiate scope.
That said, i find this tip somewhat interesting, but dont know how to do that.
$_@_.% Guest
-
Martien Verbruggen #4
Re: Distributing an array of hash refs across a table?
On Wed, 24 Dec 2003 06:33:11 GMT,
$_@_.% <$_@_.%> wrote:Apart from warnings, you ahould also enable strictures. Put>>> [email]zaphod@earthling.net[/email] (Zaphod) writes:>>> Always enable warnings unless you've decided there's a particular
>> reason you want to disable a particular warning. If so then disable
>> the particular warning only and for the smallest appropiate scope.
> Hi sorry for butting in... but im new and the posts help me learn.
> That said, i find this tip somewhat interesting, but dont know how to do that.
use warnings;
use strict;
at the top of your Perl program or module. If you need to write for
pre-5.6.0 Perls, use the -w command line switch instead of the warnings
pragma.
You can read more in the perllexwarn and warnings documentation.
$ perldoc perllexwarn
$ perldoc warnings
Martien
--
|
Martien Verbruggen | Little girls, like butterflies, need no
| excuse - Lazarus Long
|
Martien Verbruggen Guest
-
Brian McCauley #5
Re: Distributing an array of hash refs across a table?
$_@_.% writes:
perldoc perllexwarn>> > [email]zaphod@earthling.net[/email] (Zaphod) writes:>> > Always enable warnings unless you've decided there's a particular
> > reason you want to disable a particular warning. If so then disable
> > the particular warning only and for the smallest appropiate scope.
> Hi sorry for butting in... but im new and the posts help me learn.
> That said, i find this tip somewhat interesting, but dont know how to do that.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
Brian McCauley Guest



Reply With Quote

