[EMAIL PROTECTED] wrote:
> 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?
Recommended revised code at bottom.
> #!/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
> }
use strict;
my $debug = 0;
my @MYDATA = (
['BILLY BOB', '21,14902', '08:30:00 04/10/04', 'XFTRYUI', 'Main entry form'],
['Jimmy Joe', '32,25013', '09:31:01 05/11/05', 'YGUSZVJ', 'Secondary form'],
);
userdat_print ([EMAIL PROTECTED]);
exit;
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub userdat_print {
my $ary1 = shift;
my @mancols = ('USER', 'SID,SERIAL', 'LOGON TIME', 'MODULE', 'ACTION');
my @mansep = ('~~~~', '~~~~~~~~~~', '~~~~~~~~~~', '~~~~~~', '~~~~~~');
my ($format, @colsizes) = format_output ([EMAIL PROTECTED], $ary1);
print "format='$format', colsizes='@colsizes'\n" if $debug;
printf "$format\n", @mancols;
printf "$format\n", @mansep;
printf "$format\n", @$_ foreach @{$ary1};
}
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub format_output {
my ($ary1, $ary2) = @_;
# determine max column width for data lines
my $index = 0;
my @maxcollength;
foreach my $line (@$ary2) {
$index = 0;
foreach my $column (@$line) {
my $len = length $column;
$maxcollength[$index] = 0 unless defined $maxcollength[$index];
if ($len > $maxcollength[$index]) {
$maxcollength[$index] = $len;
}
printf "Index: %s, Len: %s, Data: %s, Max=%s\n", $index,
$len, $column, $maxcollength[$index] if $debug;
$index++;
}
}
# determine max column width for header lines
$index = 0;
foreach my $headers (@$ary1) {
my $len = length $headers;
$maxcollength[$index] = $len unless (defined ($maxcollength[$index]) &&
$maxcollength[$index] > $len);
$index++;
}
# create printf format for each line of data
my $format = '';
foreach my $width (@maxcollength) {
$format .= ' ' if $format;
$format .= "%-${width}s";
}
return $format, @maxcollength;
}
__END__
--
,-/- __ _ _ $Bill Luebkert Mailto:[EMAIL PROTECTED]
(_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED]
/ ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs