For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > May 2004 > Displaying Data in a Table









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Displaying Data in a Table
Steve Baek

2004-05-22, 11:32 am

Fellow Perl-Mongers:

I'm trying to break apart the command `df -k` data and display it into an
html table:

Filesystem kbytes used avail capacity Mounted on
/dev/dsk/c0t2d0s0 3008783 83669 2864939 3% /
/dev/dsk/c0t2d0s3 4032654 886633 3105695 23% /usr
/proc 0 0 0 0% /proc
mnttab 0 0 0 0% /etc/mnttab
fd 0 0 0 0% /dev/fd
/dev/dsk/c0t2d0s7 19808378 250080 19360215 2% /var
swap 594216 104 594112 1% /var/run
swap 594448 336 594112 1% /tmp
/dev/dsk/c0t2d0s6 1985487 532663 1393260 28% /export
/dev/dsk/c0t2d0s5 1985487 356649 1569274 19% /opt
/dev/dsk/c0t2d0s4 7057565 914628 6072362 14% /usr/local

Would anyone happen to know the best way to do this? I'm thinking of putting
each line into an array...

Does anyone have any thoughts?

Thanks,

Steve
Wiggins D'Anconia

2004-05-22, 11:32 am

Baek, Steve wrote:
> Fellow Perl-Mongers:
>
> I'm trying to break apart the command `df -k` data and display it into an
> html table:
>
> Filesystem kbytes used avail capacity Mounted on
> /dev/dsk/c0t2d0s0 3008783 83669 2864939 3% /
> /dev/dsk/c0t2d0s3 4032654 886633 3105695 23% /usr
> /proc 0 0 0 0% /proc
> mnttab 0 0 0 0% /etc/mnttab
> fd 0 0 0 0% /dev/fd
> /dev/dsk/c0t2d0s7 19808378 250080 19360215 2% /var
> swap 594216 104 594112 1% /var/run
> swap 594448 336 594112 1% /tmp
> /dev/dsk/c0t2d0s6 1985487 532663 1393260 28% /export
> /dev/dsk/c0t2d0s5 1985487 356649 1569274 19% /opt
> /dev/dsk/c0t2d0s4 7057565 914628 6072362 14% /usr/local
>
> Would anyone happen to know the best way to do this? I'm thinking of putting
> each line into an array...
>
> Does anyone have any thoughts?
>


Consider using the Filesys::Diskfree module from CPAN.

What have you tried? Where did you fail? Getting each line into an
array is simple since the backticks will return an array when called in
list context. Of course I assume you have taint checking on, are using
full paths to the executable, checking its return code properly, etc.

CGI.pm has methods for building tables dynamically you should read its docs.

perldoc -f split

Might also prove useful.

http://danconia.org
Charles K. Clarkson

2004-05-22, 11:32 am

Baek, Steve <BaekS@npmoc-sd.navy.mil> wrote:
:
: I'm trying to break apart the command `df -k` data
: and display it into an html table:
:
: Filesystem kbytes used avail capacity Mounted on
: /dev/dsk/c0t2d0s0 3008783 83669 2864939 3% /
: /dev/dsk/c0t2d0s3 4032654 886633 3105695 23% /usr
: /proc 0 0 0 0% /proc
: mnttab 0 0 0 0% /etc/mnttab
: fd 0 0 0 0% /dev/fd
: /dev/dsk/c0t2d0s7 19808378 250080 19360215 2% /var
: swap 594216 104 594112 1% /var/run
: swap 594448 336 594112 1% /tmp
: /dev/dsk/c0t2d0s6 1985487 532663 1393260 28% /export
: /dev/dsk/c0t2d0s5 1985487 356649 1569274 19% /opt
: /dev/dsk/c0t2d0s4 7057565 914628 6072362 14% /usr/local


: Would anyone happen to know the best way to do this?

Not the "best" way, No.


: I'm thinking of putting each line into an array...

Sounds like a plan ...

The most obvious method to break this apart is using 'split'.
Since "Mounted on" contains a space we need to limit the split to
6 columns:

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper 'Dumper';

while ( <DATA> ) {
chomp;
print Dumper [ split ' ', $_, 6 ];
}

__END__
Filesystem kbytes used avail capacity Mounted on
/dev/dsk/c0t2d0s0 3008783 83669 2864939 3% /
/dev/dsk/c0t2d0s3 4032654 886633 3105695 23% /usr
/proc 0 0 0 0% /proc
mnttab 0 0 0 0% /etc/mnttab
fd 0 0 0 0% /dev/fd
/dev/dsk/c0t2d0s7 19808378 250080 19360215 2% /var
swap 594216 104 594112 1% /var/run
swap 594448 336 594112 1% /tmp
/dev/dsk/c0t2d0s6 1985487 532663 1393260 28% /export
/dev/dsk/c0t2d0s5 1985487 356649 1569274 19% /opt
/dev/dsk/c0t2d0s4 7057565 914628 6072362 14% /usr/local



CGI.pm provides a td() function for cell data. It accepts
a reference to an array for multiple cells.

use CGI qw( table Tr td );

my @rows;
while ( <DATA> ) {
chomp;
push @rows,
Tr(
td( [ split ' ', $_, 6 ] )
);
}
print table( @rows );


Or as a subroutine:

use CGI qw( table Tr td );

print table_ize( *DATA );

sub table_ize {
my $file_handle = shift;

my @rows;
while ( <$file_handle> ) {
chomp;
push @rows,
Tr(
td( [ split ' ', $_, 6 ] )
);
}
return table( @rows );
}


: Does anyone have any thoughts?

Not on a Saturday, no.

HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328

John Mooney

2004-05-22, 11:32 am

Late to the question, but don't discount using a simple <pre></pre> =
(pre-formatted) tag either ... keeps things lined up nicely in a mono-space=
d font. I often use this for short web-ified shell cmd results.
=20
#!/usr/local/bin/perl -w
=20
print "Content-type: text/html\n\n";
our @cmd =3D `df -sk|fgrep -v nfs|sort +3 -r`; ## filter out cross-mount=
ed w/fgrep -v
print <<EOF;
<html>
<head>
<title>DiskFree on Weabpp</title>
</head>
<body>
<h2>webapp disk free results</h2>
(results of df -sk | fgrep -v nfs| sort +3 -r)
<p>
<hr>
<pre>
@cmd
</pre>
<hr>
</body>
</html>
=20
EOF

FYI
=20
=20
[color=darkred]
Baek, Steve < BaekS@npmoc-sd.navy.mil > wrote:=20
:=20
: I'm trying to break apart the command `df -k` data=20
: and display it into an html table:=20
:=20
: Filesystem kbytes used avail capacity Mounted on=20
: /dev/dsk/c0t2d0s0 3008783 83669 2864939 3% /=20
: /dev/dsk/c0t2d0s3 4032654 886633 3105695 23% /usr=20
: /proc 0 0 0 0% /proc=20
: mnttab 0 0 0 0% /etc/mnttab=20
: fd 0 0 0 0% /dev/fd=20
: /dev/dsk/c0t2d0s7 19808378 250080 19360215 2% /var=20
: swap 594216 104 594112 1% /var/run=20
: swap 594448 336 594112 1% /tmp=20
: /dev/dsk/c0t2d0s6 1985487 532663 1393260 28% /export=20
: /dev/dsk/c0t2d0s5 1985487 356649 1569274 19% /opt=20
: /dev/dsk/c0t2d0s4 7057565 914628 6072362 14% /usr/local=20


: Would anyone happen to know the best way to do this?=20

Not the "best" way, No.=20


: I'm thinking of putting each line into an array...=20

Sounds like a plan ...=20

The most obvious method to break this apart is using 'split'.=20
Since "Mounted on" contains a space we need to limit the split to=20
6 columns:=20

#!/usr/bin/perl=20

use strict;=20
use warnings;=20
use Data::Dumper 'Dumper';=20

while ( <DATA> ) {=20
chomp;=20
print Dumper [ split ' ', $_, 6 ];=20
}=20

__END__=20
Filesystem kbytes used avail capacity Mounted on=20
/dev/dsk/c0t2d0s0 3008783 83669 2864939 3% /=20
/dev/dsk/c0t2d0s3 4032654 886633 3105695 23% /usr=20
/proc 0 0 0 0% /proc=20
mnttab 0 0 0 0% /etc/mnttab=20
fd 0 0 0 0% /dev/fd=20
/dev/dsk/c0t2d0s7 19808378 250080 19360215 2% /var=20
swap 594216 104 594112 1% /var/run=20
swap 594448 336 594112 1% /tmp=20
/dev/dsk/c0t2d0s6 1985487 532663 1393260 28% /export=20
/dev/dsk/c0t2d0s5 1985487 356649 1569274 19% /opt=20
/dev/dsk/c0t2d0s4 7057565 914628 6072362 14% /usr/local=20



CGI.pm provides a td() function for cell data. It accepts=20
a reference to an array for multiple cells.=20

use CGI qw( table Tr td );=20

my @rows;=20
while ( <DATA> ) {=20
chomp;=20
push @rows,=20
Tr(=20
td( [ split ' ', $_, 6 ] )=20
);=20
}=20
print table( @rows );=20


Or as a subroutine:=20

use CGI qw( table Tr td );=20

print table_ize( *DATA );=20

sub table_ize {=20
my $file_handle =3D shift;=20

my @rows;=20
while ( <$file_handle> ) {=20
chomp;=20
push @rows,=20
Tr(=20
td( [ split ' ', $_, 6 ] )=20
);=20
}=20
return table( @rows );=20
}=20


: Does anyone have any thoughts?=20

Not on a Saturday, no.=20

HTH,=20

Charles K. Clarkson=20
--=20
Mobile Homes Specialist=20
254 968-8328=20



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com