Michael Gale wrote:
Hello,

Hello,

I am trying to pass an array by reference since it could become very large. The function should take the array and print it out but only 10 elements per line.

Did you read the Perl subroutines and/or the Perl references document?

perldoc perlsub
perldoc perlref


#!/usr/bin/perl -w

use strict;
use warnings;

use DBI;

......

sub printlist
{

my $arrayref = @_;

An array in scalar context returns the numbers of elements of that array (hence the error message 'Can't use string ("1") as an ARRAY ref') so that should be written as:

        my ( $arrayref ) = @_;

Or:
        my $arrayref = $_[0];

Or:
        my $arrayref = shift;


my $max = $arrayref;

In order to get the size of the array you need to dereference it:

        my $max = @$arrayref;


        my $nmax;
        for(my $i = 0; $i <= $max; $i++) {
                            ^^
You are accessing one element past the end of the array.


                print "\t\t\t\t\t";

                if ( ($max - $i) >= 10 ) {
                        $nmax = 10;
                } else {
                        $nmax = $max - $i;
                }

                for(my $n = 0; $n <= $nmax; $n++) {
                        print "$arrayref->[$i]\t";
# line 209              $n++;
                        $i++;
                }
                print "\n";
        }

for ( my $i = 0; $i < @$arrayref; $i += 10 ) { print "\t\t\t\t\t", join( "\t", @{$arrayref}[ $i .. $i + 9 ] ), "\n"; }


}

......

&printlist([EMAIL PROTECTED]);
  ^
You probably don't need the special behaviour that the ampersand provides.


John -- use Perl; program fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to