Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > macOS > Any 1337 Perl guys around?

Any 1337 Perl guys around?
Thread Tools
kelesh
Dedicated MacNNer
Join Date: Mar 2003
Location: boston, ma
Status: Offline
Reply With Quote
Jul 28, 2003, 03:51 PM
 
I have a script that outputs a list of text(usernames), and I want to make it into a pretty 3xN grid(3 columns 10 characters wide each, and as many rows as it takes). How can I do this in perl? I am learning perl but I don't know nearly enough to do this yet.

script1 outputs a list of names, 1 on each line. this would be script2.

i want:
script1 | script2 to output the list in with names in 3 columns, with some centering. For a good description of the problem, with an example and method: http://web.mit.edu/kelesh/Public/formatting-script

I wrote the step-by-step method down there, I just need to put it into perl code and it will work.
     
Paul McCann
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status: Offline
Reply With Quote
Jul 29, 2003, 12:52 AM
 
Err, nup to the l337 claim, but maybe this will get you started toward what you're after. If you save it as, say, "table.pl" in your ~/bin directory, and "chmod u+x ~/bin/table.pl", then "rehash", you should be able to just pipe
your list of names to it (as required).

Cheers,
Paul

Code:
#!/usr/bin/perl -w use strict; while(<>){ chomp; my $l=length(); # number of characters in the username my $pre=int(5-($l+1)/2); # amount of "pre" space print " "x$pre.$_." "x(10-$pre-$l); print "\n" unless ($.%3); }
     
Wevah
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
Jul 29, 2003, 12:45 PM
 
Originally posted by Paul McCann:
Err, nup to the l337 claim, but maybe this will get you started toward what you're after. If you save it as, say, "table.pl" in your ~/bin directory, and "chmod u+x ~/bin/table.pl", then "rehash", you should be able to just pipe
your list of names to it (as required).

Cheers,
Paul

Code:
#!/usr/bin/perl -w use strict; while(<>){ chomp; my $l=length(); # number of characters in the username my $pre=int(5-($l+1)/2); # amount of "pre" space print " "x$pre.$_." "x(10-$pre-$l); print "\n" unless ($.%3); }
You could just use

Code:
printf "%-10s%-10s%-10s\n",$foo,$bar,$baz;
for one row, and not have to do all that length calculation...
[Wevah setPostCount:[Wevah postCount] + 1];
     
kelesh  (op)
Dedicated MacNNer
Join Date: Mar 2003
Location: boston, ma
Status: Offline
Reply With Quote
Jul 29, 2003, 04:02 PM
 
wevah, i'm not sure i understand that code but it probably works.

paul, thanks alot man, you're 1337 in my book

i got ahold of "learn perl" by oreilly. this stuff looks awesome.
     
Wevah
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
Jul 29, 2003, 04:58 PM
 
Check a Perl reference for printf. It prints out formatted strings, so you can use it to avoid having to calculate the length of your strings.
[Wevah setPostCount:[Wevah postCount] + 1];
     
Paul McCann
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status: Offline
Reply With Quote
Jul 29, 2003, 08:08 PM
 
Originally posted by Wevah:
Check a Perl reference for printf. It prints out formatted strings, so you can use it to avoid having to calculate the length of your strings.
Umm, I think you've missed the bit about the output having to be centred (or as near as possible to centred) within a ten character "column". That's what I'm calculating above. The -10%s printf specification just left justifies the output within that column, and unless I'm mistaken there's no way of saying "centre this" within printf.

Cheers,
Paul
     
Zim
Senior User
Join Date: Apr 2001
Location: Cary, NC
Status: Offline
Reply With Quote
Jul 29, 2003, 11:30 PM
 
Code:
#!/usr/bin/perl -w use strict; while(<>){ chomp; my $l=length(); # number of characters in the username my $pre=int(5-($l+1)/2); # amount of "pre" space print " "x$pre.$_." "x(10-$pre-$l); print "\n" unless ($.%3); }
Paul, I've been a perl hacker for several years, but I find that you never know what you don't know to even look for, so I always look over these snippets to see if I can learn something new...

Just learned about what I'll call "print x times", and about $. Good stuff!

thanks!
Mike
     
Paul McCann
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status: Offline
Reply With Quote
Jul 30, 2003, 08:03 AM
 
Originally posted by Zim:

Just learned about what I'll call "print x times", and about $. Good stuff!
Nice to know! You're right, useful little worms pop out of the woodwork every now and then: Perl seems to have more nooks and crannies than most languages, which seems to generate sneers from language purists, but smiles from those of us who enjoy a little perversity!

Cheers,
Paul
     
Wevah
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
Jul 31, 2003, 08:42 AM
 
Originally posted by Paul McCann:
Umm, I think you've missed the bit about the output having to be centred (or as near as possible to centred) within a ten character "column". That's what I'm calculating above. The -10%s printf specification just left justifies the output within that column, and unless I'm mistaken there's no way of saying "centre this" within printf.

Cheers,
Paul
Yes, you're right, I did. Seems kind of silly to center names in a multicolumn layout (to me) though.

Sorry.
[Wevah setPostCount:[Wevah postCount] + 1];
     
LordJavac
Forum Regular
Join Date: Oct 2000
Location: Portland, OR USA
Status: Offline
Reply With Quote
Aug 1, 2003, 02:18 PM
 
Just to enlighten some to a feature of perl, you could also use a format as in:
Code:
#!/usr/bin/perl -w format STDOUT = @|||||||||| @|||||||||| @|||||||||| @items . while (<>) { chomp; push @items, $_; if (@items == 3) { write; @items = (); } } push @items, '' while (@items < 3); write;
     
Paul McCann
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status: Offline
Reply With Quote
Aug 4, 2003, 01:50 AM
 
Thanks for the 'format' reminder. It's been many a long year since I used "write" in this way, and I'd forgotten how useful it can be. Anyone looking to make multipage reports in good old ascii should also have a close look at Damian Conway's Text::Reform module, which does similar things to the built in format facility, but with more finesse and more flexibility. Umm, let's just say it's *better* ; (for example, it allows you to use footers easily).

Cheers,
Paul
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 08:44 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,