Colin Johnstone wrote:
>
> Gidday All,
Hello,
> A couple of you helped me with this before but I have misplaced my notes I am
> trying to pass an array to a subroutine and am having trouble.
>
> As i am passing other variables as well I know I need to pass it by reference.
> Am I doing it correctly.
>
> $pageContent = format_indexPage( \@picDetails, 0 );
So far so good.
> sub format_indexPage{
> my ( $paramArrayPicDetails, $paramIndexPageId ) = @_;
^^^^^^^^^^^^^^^^^^^^^
This is now a reference to an array.
> my $output = "";
>
> $output .= "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\"
>summary=\"\">";
Why not use different quoting so you don't have to backslash all the double quotes.
Since nothing in the string is interpolated you could use single quotes:
my $output = '<table width="100%" border="0" cellspacing="0" cellpadding="5"
summary="">';
> my $fromRow = $paramIndexPageId * 3;
> my $toRow = ($paramIndexPageId * 3) + 3;
my $toRow = $fromRow + 3;
> $numElements = scalar( @paramArrayPicDetails );
^^^^^^^^^^^^^^^^^^^^^
You have to dereference the array reference.
$numElements = @$paramArrayPicDetails;
> iwpt_output("num elements array = $numElements<br>");
> iwpt_output("from row $fromRow to row $toRow<br>");
>
> for( $rowPointer = $fromRow; $rowPointer < $toRow; $rowPointer++ ){
A more Perlish way to do that:
for my $rowPointer ( $fromRow .. $toRow - 1 ){
> my $dataCell0 = " ";
> my $dataCell1 = " ";
> my $dataCell2 = " ";
> my $dataCell3 = " ";
my ( $dataCell0, $dataCell1, $dataCell2, $dataCell3 ) = (' ') x 4;
But why not use an array instead of four separate scalars?
my @dataCell = (' ') x 4;
> if( defined( $paramArrayPicDetails[ $rowPointer * 4 + 0 ] ) ){
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> $dataCell0 = format_cellData( $paramArrayPicDetails[ $rowPointer * 4 + 0 ] );
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You have to dereference the array reference.
if( defined( $paramArrayPicDetails->[ $rowPointer * 4 + 0 ] ) ){
$dataCell0 = format_cellData( $paramArrayPicDetails->[ $rowPointer * 4 + 0 ] );
> }
>
> if( defined( $paramArrayPicDetails[ $rowPointer * 4 + 1 ] ) ){
> $dataCell1 = format_cellData( $paramArrayPicDetails[ $rowPointer * 4 + 1 ] );
> }
>
> if( defined( $paramArrayPicDetails[ $rowPointer * 4 + 2 ] ) ){
> $dataCell2 = format_cellData( $paramArrayPicDetails[ $rowPointer * 4 + 2 ] );
> }
>
> if( defined( $paramArrayPicDetails[ $rowPointer * 4 + 3 ] ) ){
> $dataCell3 = format_cellData( $paramArrayPicDetails[ $rowPointer * 4 + 3 ] );
> }
>
> $output .= "\t<tr>\n";
> $output .= "\t\t<td align=\"center\" valign=\"middle\">$dataCell0</td>\n";
> $output .= "\t\t<td width=\"1%\"> </td>\n";
> $output .= "\t\t<td align=\"center\" valign=\"middle\">$dataCell1</td>\n";
> $output .= "\t\t<td width=\"1%\"> </td>\n";
> $output .= "\t\t<td align=\"center\" valign=\"middle\">$dataCell2</td>\n";
> $output .= "\t\t<td width=\"1%\"> </td>\n";
> $output .= "\t\t<td align=\"center\" valign=\"middle\">$dataCell3</td>\n";
> $output .= "\t<tr>\n";
Why not use different quoting so you don't have to backslash all the double quotes.
$output .= qq[\t<tr>\n];
$output .= qq[\t\t<td align="center" valign="middle">$dataCell0</td>\n];
$output .= qq[\t\t<td width="1%"> </td>\n];
$output .= qq[\t\t<td align="center" valign="middle">$dataCell1</td>\n];
$output .= qq[\t\t<td width="1%"> </td>\n];
$output .= qq[\t\t<td align="center" valign="middle">$dataCell2</td>\n];
$output .= qq[\t\t<td width="1%"> </td>\n];
$output .= qq[\t\t<td align="center" valign="middle">$dataCell3</td>\n];
$output .= qq[\t<tr>\n];
Or:
$output .= <<HTML;
\t<tr>
\t\t<td align="center" valign="middle">$dataCell0</td>
\t\t<td width="1%"> </td>
\t\t<td align="center" valign="middle">$dataCell1</td>
\t\t<td width="1%"> </td>
\t\t<td align="center" valign="middle">$dataCell2</td>
\t\t<td width="1%"> </td>
\t\t<td align="center" valign="middle">$dataCell3</td>
\t<tr>
HTML
> }
>
> $output .= "</table>";
>
> return $output;
> }
Here is a version that eliminates most redundant code and data:
sub format_indexPage {
my ( $paramArrayPicDetails, $paramIndexPageId ) = @_;
my $output = '<table width="100%" border="0" cellspacing="0" cellpadding="5"
summary="">';
my $fromRow = $paramIndexPageId * 3;
my $toRow = $fromRow + 2;
my $numElements = @$paramArrayPicDetails;
iwpt_output( "num elements array = $numElements<br>" );
iwpt_output( "from row $fromRow to row $toRow<br>" );
for my $rowPointer ( $fromRow .. $toRow ) {
$rowPointer *= 4;
my @dataCell;
for my $num ( 0 .. 3 ) {
push @dataCell, qq(\t\t<td align="center" valign="middle">)
. ( defined $paramArrayPicDetails->[ $rowPointer + $num ]
? format_cellData( $paramArrayPicDetails->[ $rowPointer + $num ] )
: ' ' )
. qq(</td>\n);
}
$output .= "\t<tr>\n"
. join( qq(\t\t<td width="1%"> </td>\n), @dataCell )
. "\t<tr>\n";
}
$output .= '</table>';
return $output;
}
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]