gpt4 book ai didi

perl - "Redundant argument in printf"警告

转载 作者:行者123 更新时间:2023-12-04 11:54:47 25 4
gpt4 key购买 nike

为什么我在 Perl 中收到此错误“printf 中的冗余参数”?这表示格式字符串需要的值多于包含的值。我提供的格式字符串是否正确?
输入文件:

<?xml version='1.0'?>
<playlist>
<movie id="tt0112384">
<title>Apollo 13</title>
<director>Ron Howard</director>
<release-date>1995-06-30</release-date>
<mpaa-rating>PG</mpaa-rating>
<running-time>140</running-time>
<genre>adventure</genre>
<genre>drama</genre>
<cast>
<person name="Tom Hanks" role="Jim Lovell" />
<person name="Bill Paxton" role="Fred Haise" />
<person name="Kevin Bacon" role="Jack Swigert" />
<person name="Gary Sinise" role="Ken Mattingly" />
<person name="Ed Harris" role="Gene Kranz" />
</cast>
<imdb-info url="http://www.imdb.com/title/tt0112384/">
<synopsis>
NASA must devise a strategy to return Apollo 13 to Earth safely
after the spacecraft undergoes massive internal damage putting
the lives of the three astronauts on board in jeopardy.
</synopsis>
<score>7.6</score>
</imdb-info>
</movie>
<movie id="tt0307479">
<title>Solaris</title>
<director>Steven Soderbergh</director>
<release-date>2002-11-27</release-date>
<mpaa-rating>PG-13</mpaa-rating>
<running-time>99</running-time>
<genre>drama</genre>
<genre>mystery</genre>
<genre>romance</genre>
<cast>
<person name="George Clooney" role="Chris Kelvin" />
<person name="Natascha McElhone" role="Rheya" />
<person name="Ulrich Tukur" role="Gibarian" />
</cast>
<imdb-info url="http://www.imdb.com/title/tt0307479/">
<synopsis>
A troubled psychologist is sent to investigate the crew of an
isolated research station orbiting a bizarre planet.
</synopsis>
<score>6.2</score>
</imdb-info>
</movie>
</playlist>
我的代码:
use warnings;
use XML::LibXML;
my $filename = 'playlist.xml';
my $dom = XML::LibXML->load_xml(location => $filename);
printf("%-20s %-35s %-10s %-10s\n", "Title: ", "Director: ", "Rating: ", "Duration: ");
foreach my $movie ($dom->findnodes('//movie')) {
printf("%-20s %-35s %-10s %-10s\n", # <-- This is line 7
$movie->findvalue('./title'),
$movie->findvalue('./director'),
$movie->findvalue('./mpaa-rating'),
$movie->findvalue('./running-time'),
" minutes");
}
预期输出:
Title:          Director:       Rating:         Duration:
Apollo 13 Ron Howard PG 140
Solaris Steven Soderbergh PG-13 99
Ender's Game Gavin Hood PG-13 114
Interstellar Christopher Nolan PG-13 169
The Martian Ridley Scott PG-13 144
获取输出:
Title:                    Director:                 Rating:                   Duration:
Redundant argument in printf at movie.pl line 7.
Apollo 13 Ron Howard PG 140
Redundant argument in printf at movie.pl line 7.
Solaris Steven Soderbergh PG-13 99
Redundant argument in printf at movie.pl line 7.
Ender's Game Gavin Hood PG-13 114
Redundant argument in printf at movie.pl line 7.
Interstellar Christopher Nolan PG-13 169
.
.
.
为什么 Perl 会这样?

最佳答案

printf :

printf("%-20s %-35s %-10s %-10s\n", 
$movie->findvalue('./title'),
$movie->findvalue('./director'),
$movie->findvalue('./mpaa-rating'),
$movie->findvalue('./running-time'),
" minutes");
需要 4 个参数,因为它包含 4 %<something> .但是,您提供了 5 个参数。你可以把 minutes在格式字符串中(并将其从参数中删除):
printf("%-20s %-35s %-10s %-10s minutes\n", 
$movie->findvalue('./title'),
$movie->findvalue('./director'),
$movie->findvalue('./mpaa-rating'),
$movie->findvalue('./running-time'));
或者,如果您想要 minutes要紧邻分钟数,将其连接到 $movie->findvalue('./running-time') :
printf("%-20s %-35s %-10s %-10s\n", 
$movie->findvalue('./title'),
$movie->findvalue('./director'),
$movie->findvalue('./mpaa-rating'),
$movie->findvalue('./running-time') . " minutes");
可能让您感到困惑的是 print接受要打印的参数列表,而 printf接受格式字符串和要替换的参数列表,并在格式字符串中进行格式化。这样做就好了:
print($movie->findvalue('./title'), 
$movie->findvalue('./director'),
$movie->findvalue('./mpaa-rating'),
$movie->findvalue('./running-time'),
" minutes\n");
(尽管这不会按照您的意愿格式化输出)

关于perl - "Redundant argument in printf"警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68360258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com