Perl 格式 (iesprintf) 未保留在 html 显示中

发布于 2024-08-06 20:52:40 字数 725 浏览 9 评论 0原文

我遇到了一些问题。最初,我有以下格式的输入:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久隐师 2024-08-13 20:52:40

HTML 会忽略多余的空格。事实上,它可能以比例字体显示,这意味着即使存在额外的空格,它也不会对齐。

最简单的选择是用

 包围文本。标签,默认情况下将显示为等宽字体并保留空白。或者,您可以让代码生成 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.

守不住的情 2024-08-13 20:52:40

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.

逆蝶 2024-08-13 20:52:40

 中的“pre”表示“预格式化”,它准确地描述了 sprintf() 语句的输出。因此,我怀疑弗里多和其他人也提出了建议。

The 'pre' in <pre> means preformatted, which exactly describes the output of a sprintf() statement. Hence the suggestion from friedo and I suspect, others.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文