Perl 格式 (iesprintf) 未保留在 html 显示中
我遇到了一些问题。最初,我有以下格式的输入:
12345 apple
12 orange
我将第一列保存为 $num,将第二列保存为 $fruit。我希望输出看起来像这样(见下文)。我希望输出能够对齐,就好像 $num 的长度都相同一样。实际上,$num 将由可变长度的数字组成。
12345 apple
12 orange
按照建议,我使用以下代码:
$line = sprintf "%--10s %-20s", $num, $fruit;
此解决方案在命令行显示中效果很好,但当我尝试通过 HTML 显示此格式时,不会保留此格式。例如..
print "<html><head></head><body>
$line
</body></html>";
这会产生与格式化之前的原始输出相同的输出。你们对如何在基于 html Web 的显示中保留 sprintf 格式有什么建议吗?我尝试用空格填充 $num,但以下代码似乎对我不起作用。
$num .= (" " x (10 - length($num)));
无论如何,我将不胜感激任何建议。谢谢!
I have ran into a bit of problem. Originally, I have the following input of the format:
12345 apple
12 orange
I saved the first column as $num and second column as $fruit. I want the output to look like this (see below). I would like for the output to align as if the $num are of all the same length. In reality, the $num will consists of variable-length numbers.
12345 apple
12 orange
As suggested, I use the following code:
$line = sprintf "%--10s %-20s", $num, $fruit;
This solution works great in command-line display, but this formatting is not retained when I try to display this via HTML. For example..
print "<html><head></head><body>
$line
</body></html>";
This produces the same output as the original before formatting. Do you guys have a suggestion as to how I can retain the sprintf formatting in html web-based display? I try to pad the $num with whitespaces, but the following code doesn't seem to work for me.
$num .= (" " x (10 - length($num)));
Anyways, I would appreciate any suggestions. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HTML 会忽略多余的空格。事实上,它可能以比例字体显示,这意味着即使存在额外的空格,它也不会对齐。
最简单的选择是用
HTML ignores extra whitespace. And the fact that it's probably displaying with a proportional font means it wouldn't line up even if the extra spaces were there.
The easy option is to just surround the text with <pre> tags, which will display by default with a monospace font and whitespace preserved. Alternatively, you can have your code generate an HTML table.
HTML 将所有连续空格压缩为一个空格。如果您希望输出像表格一样排列,则必须将值实际放入 HTML 表格中。
HTML compresses all consecutive spaces down to one space. If you want your output to be lined up like a table, you have to actually put the values in an HTML table.
The 'pre' in
<pre>
means preformatted, which exactly describes the output of asprintf()
statement. Hence the suggestion from friedo and I suspect, others.