more useful options | |
PerlMonks |
Re: Format questionby roboticus (Chancellor) |
on Dec 03, 2019 at 21:11 UTC ( [id://11109625]=note: print w/replies, xml ) | Need Help?? |
At first, I guessed that formatting problem is that the data lines contain whitespace which caused that first field to blow up. I was going to suggest that you tell split to ignore whitespace like the following:
But when I ran the script, I got the same result you did:
That's when I looked at your print statement: You're using a tab to try to align the columns. But a tab simply tells the print statement to go to the next column that's a multiple of 8. When your input data has width differences, you'll find that a field that's a shade too long pushes everything over eight more characters. So the solution is to use printf with a width specifier (as tobyink mentioned), or the format statement (as hippo suggested) or pack or some such. I just tweaked it to use printf since it was the simplest change:
In this version, I added a width specifier to both the customer name *and* the number of books checked out. Notice that the columns line up correctly *unless* one of the data items is too long to fit in the field (the last two lines). One way you can handle that is to tell split that you don't care about whitespace around your delimiter, like this: that the data has a bunch of whitespace in it, causing yo ...roboticus When your only tool is a hammer, all problems look like your thumb.
In Section
Seekers of Perl Wisdom
|
|