|
|
Any 1337 Perl guys around?
|
|
|
|
Dedicated MacNNer
Join Date: Mar 2003
Location: boston, ma
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status:
Offline
|
|
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);
}
|
|
|
|
|
|
|
|
|
Senior User
Join Date: Nov 2001
Location: State of Denial
Status:
Offline
|
|
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];
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Mar 2003
Location: boston, ma
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Senior User
Join Date: Nov 2001
Location: State of Denial
Status:
Offline
|
|
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];
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Senior User
Join Date: Apr 2001
Location: Cary, NC
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Senior User
Join Date: Nov 2001
Location: State of Denial
Status:
Offline
|
|
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];
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Oct 2000
Location: Portland, OR USA
Status:
Offline
|
|
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;
|
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status:
Offline
|
|
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 Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|