Path: utzoo!attcan!uunet!samsung!zaphod.mps.ohio-state.edu!mips!bridge2!mdb From: mdb@ESD.3Com.COM (Mark D. Baushke) Newsgroups: comp.lang.perl Subject: Re: Cute hack around tiny bug (Re: Help a perl apprentice) Message-ID: Date: 29 Oct 90 06:05:40 GMT References: <18840001@hp-lsd.COS.HP.COM> <21380@orstcs.CS.ORST.EDU> Sender: news@bridge2.ESD.3Com.COM Organization: 3Com Corp., Santa Clara, CA. Lines: 66 In-reply-to: pvo@sapphire.OCE.ORST.EDU's message of 28 Oct 90 04:36:52 GMT On 28 Oct 90 04:36:52 GMT, pvo@sapphire.OCE.ORST.EDU (Paul O'Neill) said: Paul> I really like Tim Chambers' df massager and Larry's rewrite of it. Paul> ------------- Paul> #!/usr/local/bin/perl Paul> open (BDF_PIPE, "bdf @ARGV |"); Paul> print < Filesystem Free (Mb) %Used Paul> ==============================|=========|===== Paul> End Paul> while () { Paul> ($fs, $kbytes, $used, $avail, $capacity, $dirname) = split; Paul> next unless $kbytes > 0; Paul> # next unless $fs; # uncomment to delete nfs filesystems Paul> $line{$avail} = sprintf("%30.30s%10.1f%6s\n", Paul> $dirname . '.' x 30, Paul> $avail / 1000, $capacity); Paul> } Paul> sub revnum { $b <=> $a; } Paul> foreach $key (sort revnum keys(%line)) { Paul> print $line{$key}; Paul> } Paul> print "\n"; Paul> ---------------------- Paul> There's a tiny bug in there, though. Paul> What if 2 file systems have the same available space? (ie. zero :-) Paul> The old $line{$avail} gets wiped by the new $line{$avail} and is not Paul> reported. I sent Larry private e-mail about this and suggested a one character change to the script change $line{$avail} = sprintf("%30.30s%10.1f%6s\n", ...); to $line{$avail} .= sprintf("%30.30s%10.1f%6s\n", ...); This works fine if you don't care about the order of two entries with the same amount of available space. Larry agreed and then suggested that another way to get around it would be to use $line{$avail.'.'.$seq++} = sprintf("%30.30s%10.1f%6s\n", ...); Paul> It occured to me that a good way to handle this was to add a Paul> small number, say 0.1, to $avail if $line{$avail} aready Paul> existed. But what if there were *three* (or more) file systems Paul> with the exact same available space? Using a linearly increasing sequence number is probably a better solution in this case. Even using rand() there is no guarentee that you will not arrive at duplicate keys. Enjoy! -- Mark D. Baushke mdb@ESD.3Com.COM