Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Creating a Dynamic Table

by ikkon (Monk)
on Jan 10, 2007 at 15:43 UTC ( #593925=perlquestion: print w/ replies, xml ) Need Help??
ikkon has asked for the wisdom of the Perl Monks concerning the following question:

I am making a PDF , which using PDF::Table I am making a table inside that PDF. What I am trying to do is make the table dynamic, cause I am passing variables and I do not want a blank table row if the variable is empty. This is what I have tried so far:
my $some_data =[ ["HP Hardware", "Total Cost"], [$hardware1, $hardwareValue1], [$hardware2, $hardwareValue2], [$hardware3, $hardwareValue3], [$hardware4, $hardwareValue4], [$hardware5, $hardwareValue5], [$hardware6, $hardwareValue6], [$hardware7, $hardwareValue7], [$hardware8, $hardwareValue8], [$hardware9, $hardwareValue9], [$hardware10, $hardwareValue10], [$hardware11, $hardwareValue11], [$hardware12, $hardwareValue12], [$hardware13, $hardwareValue13], [$hardware14, $hardwareValue14], [$hardware15, $hardwareValue15], ]; for (my $i=0; $i< scalar($some_data); $i++) { if ($hardware[$i] eq "") { } }
I am thinking if the variable is blank I want to delete the row from $some_data, I don't know the syntax to do it, I also Tried
my @tableData = $some_data; for (my $i=0; $i< scalar(@tableData); $i++) { my @row = $tableData[$i]; for ( my $j=0; $j< scalar(@row) ;$j++) { my $cell = $row[$j]; if (($cell eq "") || ($cell eq " ")) { splice(@tableData, @row, @row) } } }
Any help or suggestions would be helpful, thanks Ben

Comment on Creating a Dynamic Table
Select or Download Code
Replies are listed 'Best First'.
Re: Creating a Dynamic Table
by almut (Canon) on Jan 10, 2007 at 17:00 UTC

    I think it's probably easier to just use grep to filter the list while copying it... Something like:

    my @tableData = grep ref($_) eq "ARRAY" && # make sure we have an arrayref $_->[0] =~ /\S/ && # first cell non-empty $_->[1] =~ /\S/, # second cell non-empty ( ["Hardware A", "Total Cost A"], ["Hardware B", "Total Cost B"], ["Hardware C", ""], ["", "Total Cost D"], [" ", "Total Cost E"], ["Hardware F", " "], [" ", ""], ["Hardware H", "Total Cost H"], ); # to print out use Data::Dumper; print Dumper \@tableData;

    (The literal strings I put in above can of course also be variables...)

    Or, if you wanted to use $some_data, as you declared it, it would look like

    my @tableData = grep ref($_) eq "ARRAY" && # make sure we have an arrayref $_->[0] =~ /\S/ && # first cell non-empty $_->[1] =~ /\S/, # second cell non-empty @$some_data;
Re: Creating a Dynamic Table
by ikkon (Monk) on Jan 10, 2007 at 18:03 UTC
    thanks for your help, I fix it by using the following code
    my @tableData = ( ["HP Hardware", "Total Cost"], ); for my $i ( 1 .. 15 ) { my $hardware_name = param('HW_' . $i . '_0'); my $hardware_val = param('HW_' . $i . '_1'); if ( $hardware_name and $hardware_val =~ /\w/ ) { push @tableData, [ $hardware_name, $hardware_val ]; push @tableData, [ "Total hardware cost:", $TotalHWValue]; } # if hardware_name } # foreach i my $some_data = \@tableData;
    thanks again for your help
Re: Creating a Dynamic Table
by bobf (Monsignor) on Jan 11, 2007 at 06:24 UTC

    While it does not directly answer your question, I wanted to comment on style. In general, using variable names like this:

    [$hardware1, $hardwareValue1], [$hardware2, $hardwareValue2], [$hardware3, $hardwareValue3],
    is a bad idea. See Why it's stupid to use a variable as as a variable name for more information.

    You can use arrays, hashes, or some combination thereof to accomplish the same thing, and your data structures will be much more robust. For example:

    # array my @hardware; $hardware[1] = 'whatever'; # hash my %hardware; $hardware{1} = 'whatever'; # hash of hashes my %hardware; $hardware{1} = { hw => 'whatever', value => 'somevalue' }; # hash of arrays my %hardware; $hardware{1} = [ 'whatever', 'somevalue' ]; # array of arrays my @hardware; $hardware[1] = [ 'whatever', 'somevalue' ]; # array of hashes my @hardware; $hardware[1] = { hw => 'whatever', value => 'somevalue' };

    See perldsc for more information on complex data structures.

    HTH

      that is a very good point thanks for bringing that to my attention, I will have to change that, thanks again

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://593925]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2015-07-30 02:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls