Distributing an array of hash refs across a table?

Ask a Question related to PERL Modules, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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
    3. 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...
    4. 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...
    5. 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 =...
  3. #2

    Default Re: Distributing an array of hash refs across a table?

    [email]zaphod@earthling.net[/email] (Zaphod) writes:
    > 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....
    Thowing random solutions at a problem is not a good approach. If you
    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.
    > Thanks for any help anyone might offer...
    If you are interested in distribuiting stuff over lists then
    learn about the map function.
    > &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'}),
    > )
    > )
    > }
    I suspect you meant somthing like:

    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

  4. #3

    Default Re: Distributing an array of hash refs across a table?

    > [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.
    $_@_.% Guest

  5. #4

    Default Re: Distributing an array of hash refs across a table?

    On Wed, 24 Dec 2003 06:33:11 GMT,
    $_@_.% <$_@_.%> wrote:
    >> [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.
    Apart from warnings, you ahould also enable strictures. Put

    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

  6. #5

    Default Re: Distributing an array of hash refs across a table?

    $_@_.% writes:
    > > [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.
    perldoc perllexwarn

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
    Brian McCauley Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139