我可能在这里属于少数,但我非常喜欢Perl's formats。我特别喜欢能够在列中包装一段长文本(“~~ ^<<<<<<<<<<<<<<<<<<<<<< <“类型的东西)。是否有其他具有类似功能的编程语言或实现类似功能的库?我对任何为Ruby实现类似功能的库特别感兴趣,但我也对其他任何选项感到好奇。
答案 0 :(得分:13)
多年前我在使用它时似乎记得类似Fortran中的类似内容(但它很可能是第三方库)。
至于Perl中的其他选项,请查看Perl6::Form
。
form
函数取代了Perl6中的format
。 “Perl Best Practices”中的Damian Conway建议将Perl6::Form
与Perl5一起使用,引用format
的以下问题....
以下是Robert Gamble对Ruby示例的Perl6::Form
变体....
use Perl6::Form;
my ( $month, $day, $year ) = qw'Sep 18 2001';
my ( $num, $numb, $location, $toe_size );
for ( "Market", "Home", "Eating Roast Beef", "Having None", "On the way home" ) {
push @$numb, ++$num;
push @$location, $_;
push @$toe_size, $num * 3.5;
}
print form
' Piggy Locations for {>>>}{>>}, {<<<<}',
$month, $day, $year ,
"",
' Number: location toe size',
' --------------------------------------',
'{]}) {[[[[[[[[[[[[[[[} {].0} ',
$numb, $location, $toe_size;
答案 1 :(得分:7)
FormatR为Ruby提供类似Perl的格式。
以下是文档中的示例:
require "formatr"
include FormatR
top_ex = <<DOT
Piggy Locations for @<< @#, @###
month, day, year
Number: location toe size
-------------------------------------------
DOT
ex = <<TOD
@) @<<<<<<<<<<<<<<<< @#.##
num, location, toe_size
TOD
body_fmt = Format.new (top_ex, ex)
body_fmt.setPageLength(10)
num = 1
month = "Sep"
day = 18
year = 2001
["Market", "Home", "Eating Roast Beef", "Having None", "On the way home"].each {|location|
toe_size = (num * 3.5)
body_fmt.printFormat(binding)
num += 1
}
产生:
Piggy Locations for Sep 18, 2001
Number: location toe size
-------------------------------------------
1) Market 3.50
2) Home 7.00
3) Eating Roast Beef 10.50
4) Having None 14.00
5) On the way home 17.50
答案 2 :(得分:2)
有Lisp (format ...)
function。它支持循环,条件和一大堆其他有趣的东西。
(从上面链接复制):
(defparameter *english-list*
"~{~#[~;~a~;~a and ~a~:;~@{~a~#[~;, and ~:;, ~]~}~]~}")
(format nil *english-list* '()) ;' ==> ""
(format nil *english-list* '(1)) ;' ==> "1"
(format nil *english-list* '(1 2)) ;' ==> "1 and 2"
(format nil *english-list* '(1 2 3)) ;' ==> "1, 2, and 3"
(format nil *english-list* '(1 2 3 4));' ==> "1, 2, 3, and 4"