This is a small sub that returns the length of the longest string value in a
hashref. Arg must be passed by ref. Any additional arg causes it to look for longest key instead.
It's lovely to collaboratively code with fellow Monks. Last localtime-night in the
cb Brer'
broquaint and I wrote this subroutine which I've decided to name
max_strl (a kind of
C-ish name for a Perl identifier, granted).
Usage:
Pass the first argument/parameter as a hash reference. If any additional argument is
passed the function will return the length of the longest key instead of the
length of the longest value.
Update: Wed Aug 20 2003 23:08 UTC
Incorporated
diotalevi's
suggestion so that we can pass a blessed object that is a (
isa()) hashref, as well.
# last mod: Wed Aug 20 2003 23:08 UTC
# by [Intrepid], with quality tweaking by [broquaint] ;-)
sub max_strl {
UNIVERSAL::isa( (my $h = shift) , 'HASH')
or die "Must pass a hashref!\n";
my $ml = 0; length > $ml and $ml = length
for ( @_ ? keys %$h : values %$h );
return $ml;
}
Applications:
Although it's a very short piece of code (made even more concise due to
broquaint's
economy of style), this is IMHO a broadly useful bit. One general area where it is
likely to be helpful concerns those times in which we are trying to exercize a little
more fine-grained control over spacing in our screen real-estate. Something a little
more precise than just sprinkling our output format with
TABs ...
For (a dumb) example, to pretty-printout a hash of varying-length items like this, with
stuff on both front and behind:
#!/usr/bin/env perl
use strict;
use warnings;
$\ = "\n";
my $i = 'A';
my(%hash) = map(($i++, $_), split(/:/, $ENV{'PATH'}, 0));
my $maxl = max_strl(\%hash) + 5;
my $format1 = "PathEle %3s%${maxl}s% 10u Kb used";
foreach (sort keys %hash) {
my $use_of_disk = "du -ks $hash{$_}";
printf "$format1\n", $_, $hash{$_}, (split /\s+/,`$use_of_disk`)[0
+];
}
exit 0;
Using max_strl with printf() is just the start. Your imagination is
the only limit! {grin}.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.