Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!sdd.hp.com!hplabs!hpfcso!hpldola!hp-lsd!tbc From: tbc@hp-lsd.COS.HP.COM (Tim Chambers) Newsgroups: comp.lang.perl Subject: Help a perl apprentice Message-ID: <18840001@hp-lsd.COS.HP.COM> Date: 8 Oct 90 23:41:58 GMT Organization: HP Logic Systems Division - ColoSpgs, CO Lines: 85 I have just written my first perl script. Ok, so it's not as cryptic as TECO (which I never did master) but it's still a pretty nonintuitive language to me. So I ask the perl wizards to help me get used to programming in this new beasty. I'm enclosing the perl script, the sh script I started with, and an example of the program's input. My questions are: 1. Why doesn't the "next unless" line I've commented out do what I want it to? (Trust me, it doesn't on my HP 9000 Series 300 HP-UX implementation.) 2. Although I realize this isn't a teaching center, would anyone care to offer some advice on how to optimize my perl implementation of the algorithm? The algorithm I use is (1) capture in an array, (2) sort the array, (3) format and print the array elements. I found it clumsy to do all in perl, but it still runs faster than the filters I use in the shell script. Are there perl-isms I am missing that make the algorithm run more efficiently? (Especially my by_free sorting subroutine.) ############################################################################## #!/usr/local/bin/perl # $Header: mydf,v 1.3 90/10/08 17:17:19 tbc Exp $ sub by_free { @a = split(' ', $a); @b = split(' ', $b); $a[3] lt $b[3] ? 1 : $a[3] gt $b[3] ? -1 : 0; } open (BDF_PIPE, "bdf $* |"); # output print "Filesystem Free (Mb) %Used\n"; print "------------------------------|---------|-----\n"; for ($i=0;;$i++) { # why doesn't this work?! # next unless (/^.*\%.*$/); next unless /^[^ ]+ /; next if /Mounted/; @lines[$i] = $_; }; close (BDF_PIPE); @sortedlines = sort by_free @lines; foreach $line (@sortedlines) { ($f1, $f2, $f3, $avail, $capacity, $dirname) = split(' ', $line); printf '%s', $dirname; for ($i = 30; $i > length($dirname); $i--) { printf ('.'); } printf "%10.1f%6s\n", $avail / 1000, $capacity; }; print "\n"; ############################################################################## #!/bin/sh # $Header: mydf,v 1.2 90/09/06 08:42:15 tbc Exp $ echo "Filesystem Free (Mb) %Used" echo "------------------------------|---------|-----" # first filter to get around bdf's "pretty" format, then use awk bdf $* | cut -c36-80 | fgrep '%' | sort -nr | \ awk '{ printf("%s", $3) for (i = 30 ; i > length($3) ; i --) printf(".") printf ("%10.1f%6s\n", $1 / 1000, $2) }' echo ############################################################################## # this is what "bdf" does, in case it's not on non-HP-UX systems $ bdf Filesystem kbytes used avail capacity Mounted on /dev/root 487022 405811 32508 93% / /dev/dsk/17s7 278082 240761 23416 91% /dsk17.07 /dev/dsk/17s3 278082 202040 48233 81% /users4 /dev/dsk/0s2 278082 188859 61414 75% /disk3 /dev/dsk/0s4 119429 74980 32506 70% /users2 /dev/dsk/0s3 230127 166056 41058 80% /users hp-lsd:/usr/spool/notes 387657 248645 131258 65% /usr/spool/notes hplsdry:/work2/hp64000/hptwk/twk_db 90607 69076 17000 80% /usr/hp64000/hptwk/twk_db