This is a code snippet from my program the prints a fixed set of column headings and
seperators for an array retrieved from a database. The final output for this example
should be:
USER SID,SERIAL LOGON TIME MODULE ACTION
~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~ ~~~~~~
BILLY BOB 21,14902 08:30:00 04/10/04 XFTRYUI Main entry form
The program achieves the first part but even after dereferencing for the line data it
appears I have another reference. Problems have arose because I am trying to
accomodate the ability to pass references to ~anonymous arrays~ as well. I'm confused
here and it seems I am making references to references. Do I need to determine the
type of reference passed to my function and then code accordingly?
#!/usr/bin/perl -w
use strict;
my @MYDATA=('BILLY BOB','21,14902','08:30:00 04/10/04','XFTRYUI','Main entry form');
&userdat_print([EMAIL PROTECTED]);
sub format_output {
my @maxcollength;
my $index;
my ($ary1) = $_[0]; # not understanding what purpose the ( )'s serve on left side
of this assignment
my ($ary2) = $_[1];
foreach my $line (@$ary2) {
$index = 0;
#debug
#print "\n--LINE--\n";
foreach my $column (@$line) {
$maxcollength[$index] = 0 unless (defined $maxcollength[$index]);
if (length $column > $maxcollength[$index]) {
$maxcollength[$index]=length $column;
}
#debug
#print "\nCol: " . length $column;
#print " Data: $column Max: $maxcollength[$index]\n";
$index++;
}
}
$index=0;
foreach my $headers (@$ary1) {
$maxcollength[$index]=length($headers)
unless (defined ($maxcollength[$index]) && $maxcollength[$index]
> length($headers));
$index++;
}
my $format = "";
foreach my $width (@maxcollength) {
$format .= " " if ($format);
$format .= "%-" . $width . "s";
}
return $format, @maxcollength;
}
sub userdat_print() {
my $ary1 = [EMAIL PROTECTED]; #passed [EMAIL PROTECTED] array reference
but seem to have to reference @_.
#$_[0] seems to muck things up for me the way I have
things coded.
my @mancols=('USER','SID,SERIAL','LOGON TIME','MODULE','ACTION');
my @mansep=('~~~~','~~~~~~~~~~','~~~~~~~~~~','~~~~~~','~~~~~~');
my ($format,@colsizes)=format_output([EMAIL PROTECTED], $ary1);
#format_output is fine with $ary1 as is also when called from from other
subs and being passed
#anonymous array references, specifically in the second parameter.
print sprintf "$format\n", @mancols;
print sprintf "$format\n", @mansep;
print sprintf "$format\n", @$ary1; #problem area
}
_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs