Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Text Columns

by swampyankee (Parson)
on Nov 28, 2006 at 22:19 UTC ( [id://586579]=note: print w/replies, xml ) Need Help??


in reply to Text Columns

I believe you would use Term::Cap to find the terminal's width. In some systems, it may be directly available as an environment variable (COLUMNS)

You would then determine the length of your longest field (in your sample case, it would be the length of the string "Wednesday", plus 3 (for the colon and the two spaces that should follow a colon), plus 2 (for the length of '12' as a string), plus the padding you want between columns (you could use tabs, too). Figure out how many will fit (if you use tabs, which means you'll have to find out how wide a tab is in your output terminal. It's usually, but not always, 8). Build a format for sprintf or printf, and loop through the hash until finished. The lengths can be determined while the hash is being built. To force left-justification of a string, use something like "%-10s".

emc

At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

—Igor Sikorsky, reported in AOPA Pilot magazine February 2003.

Replies are listed 'Best First'.
Re^2: Text Columns
by geoffeg (Acolyte) on Nov 28, 2006 at 22:29 UTC
    This is what I've basically done for now. There are some edge cases that this doesn't handle (I'll explain below). I'm sorta surprised that there is no perl module to do this already so maybe it's time I wrote one. I'd like to take this even further and be able to specify formats printf-like for the values.. It gets complicated quickly when I start thinking about fancy features...

      Sorry my assistance was too elementary. I spend a lot of programming time dealing with i/o issues (it's not uncommon for one of my non-Perl programs to have over 75% of the SLOC dealing with i/o), and writing format statements on the fly is one of my favorite -- and oldest -- tricks in handling i/o.

      emc

      At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

      —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.
Re^2: Text Columns
by CountZero (Bishop) on Nov 28, 2006 at 22:35 UTC
    That's OK if all columns are to be of equal width, but if you want to have the maximum number of columns on the screen it gets much more complicated. I'm not even sure there exists a nice algoritm for it. I have a feeling it will get down to trial & error.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://586579]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-19 03:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found