Re: [ILUG] Perl thing of the day

From: Padraig Brady (padraig at domain antefacto.com)
Date: Thu 18 Oct 2001 - 16:28:43 IST


Cool, how about the shell equivalent:

while read num label; do printf "%s\t%*s\n" "$label" "$num" | tr ' '
'*'; done

You could for e.g. get a barchart representation of filesizes in the
current dir like:

max_val=`find -maxdepth 1 -printf "%s\n" | sort +0n | tail -1`
scale_val=`expr $max_val / 70`
find -maxdepth 1 -type f -printf "%s %p\n" |
while read num label; do snum=`expr $num / $scale_val + 1`; printf
"%s\t%*s\n" "$label" $snum | tr ' ' '*'; done

Padraig.

Fergal Daly wrote:

>I'm sure this exact piece of Perl has been written thousands of times by a
>other people but I wrote it today and I think it's lovely
>
>perl -pe 's/.*?(\d+)(.*)/"$2 ".("*" x $1)/ge'
>
>will draw a histogram. It takes lines of the form
>
>123 thing
>
>and produces lines of the form
>
>thing **********
>
>where there are 123 *s
>
>Got a list of usernames that have logged in over the last week?
>
>sort list | uniq -c | perl -pe 's/.*?(\d+)(.*)/"$2 ".("*" x $1)/ge'
>
>will graph them.
>
>I had a bunch of account numbers generated by some unknown algorithm and I
>wanted to know would it be good idea to to use the last 2 characters as a
>hash. So I grabbed out the last two chars of each one and luckily enough
>
>sort last_two | uniq -c | cut -f 1 | sort | uniq -c | perl -pe 's/.*?(\d+)(.*)/"$2 ".("*" x $1)/ge'
>
>gave me a nice tight bell curve.
>
>I love Perl :-)
>
>Fergal
>
>



This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:12:49 GMT