Unwrapping the second gift brought to you by Perl 6 this Advent, we find… a method named .fmt.
If you’re familiar with sprintf, you’ll feel right at home with .fmt. If you haven’t heard about sprintf before, or if you’ve heard of it but are a bit fuzzy on the details, you might want to skim the perldoc page. Don’t drown in it, though; it’s longish. Just savour it.
Back to .fmt, sprintf‘s spunky little sister. Here are a few ways to use .fmt to format strings and integers.
say 42.fmt('%+d') # '+42'
say 42.fmt('%4d') # ' 42'
say 42.fmt('%04d') # '0042'
say :16<1337f00d>.fmt('%X') # '1337F00D'
All this is good and well, but not really more than a shorter method form of sprintf. Big deal, right?
What I haven’t told you yet is that .fmt is overloaded, and works differently on arrays (or more precisely, lists):
say <huey dewey louie>.fmt # 'huey dewey louie'
say <10 11 12>.fmt('%x') # 'a b c'
say <1 2 3>.fmt('%02d', '; ') # '01; 02; 03'
Similarly, it’s overridden on hashes (or rather, mappings):
say { foo => 1, bar => 2 }.fmt # 'foo 1
# bar 2'
say { Apples => 5, Oranges => 10 }.fmt('%s cost %d euros')
# 'Apples cost 5 euros
# Oranges cost 10 euros'
say { huey => 1, dewey => 2, louie => 3 }.fmt('%s', ' -- ')
# 'huey -- dewey -- louie'
The way hashing works may give your output a different order than the ones shown above. Oh, and there’s an overloaded .fmt for pairs as well, but it works analogously to the one for hashes.
.fmt is a useful little tool to have when you want to change some value, or an array or a hash of values, into to some given format. It’s like sprintf, but tailored to Do What You Mean for arrays and hashes, too.
There’s only one risk in all of this: Perl 6 might soil the reputation of the Perl family of languages by simply being too darn readable. In order to counter this risk, I leave a small parting gift in the form of a simple-but-dense Christmas tree printing Perl 6 one liner:
$ perl6 -e 'say " "x 9-$_,"#"x$_*2-1 for 0..9,2 xx 3'
#
###
#####
#######
#########
###########
#############
###############
#################
###
###
###
c:\>perl6.exe -e "say ' 'x 9-$_,'#'x$_*2-1 for 0..9,2 xx 3"Tags: formatting, perl6
December 2, 2009 at 3:49 am |
Excellent article!!
Is there any way to have fmt produce “huey, dewey, and loiue” from ?
Seems like that one is needed alot too.
December 2, 2009 at 11:02 pm |
Great article indeed. What a good resource initiative for Perl 6. Keep it up!
December 3, 2009 at 12:01 am |
Aloha, Will! There’s no built-in way to do what you want, but one can easly write a custom .fmt method that has a different last separator: I wrote one here just to make sure it works.
December 3, 2009 at 5:05 am |
Carl, Thanks for the code!! I’m just now starting to see the light at the end of the Perl6 tunnel.
December 7, 2009 at 2:09 am |
Cool! But use .kv to get index from array in for statement, it will look much more perl6ish :)
December 7, 2009 at 6:14 am
I started out writing it that way, but ran into unexpected trouble with Rakudo. And after I changed it, people told me this version was clearer. Though in retrospect, I think I agree that .kv would have been both more idiomatic and nicer. :)
December 8, 2009 at 6:44 pm |
[...] перевод на русский язык второй статьи (Day 2: The beauty of formatting) цикла Perl 6 Advent Calendar. Статья посвящена форматированию в [...]
December 15, 2009 at 11:25 pm |
[...] рассказано об установке Rakudo, использованию циклов, операторах форматирования, статических типах, мета-операторах и приемах [...]
September 27, 2011 at 4:52 pm |
[...] 注:很可惜好象目前最新的 Rakudo 是没法执行这个小临别礼物。 本文为译文 By carl: http://perl6advent.wordpress.com/2009/12/02/day-2-the-beauty-of-formatting/ [...]