Why am I getting this error "Redundant argument in printf" in Perl? This signifies that the format string requires more values than are contained in. Is the format string I provided right?
Input file:
<?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>
My code:
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");
}
Expected Output:
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
Getting Output:
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
.
.
.
Why is Perl behaving this way?
This printf
:
printf("%-20s %-35s %-10s %-10s\n",
$movie->findvalue('./title'),
$movie->findvalue('./director'),
$movie->findvalue('./mpaa-rating'),
$movie->findvalue('./running-time'),
" minutes");
Expects 4 arguments since it contains 4 %<something>
. However, you are providing 5 arguments. You can put minutes
in the format string (and remove it from the arguments):
printf("%-20s %-35s %-10s %-10s minutes\n",
$movie->findvalue('./title'),
$movie->findvalue('./director'),
$movie->findvalue('./mpaa-rating'),
$movie->findvalue('./running-time'));
Or, if you want minutes
to be right next to the number of minutes, concatenate it to $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");
What may have confused you is that print
takes a list of arguments to print, while printf
takes a format string and a list of arguments to substitute and format inside the format string. It would have been fine to do:
print($movie->findvalue('./title'),
$movie->findvalue('./director'),
$movie->findvalue('./mpaa-rating'),
$movie->findvalue('./running-time'),
" minutes\n");
(although that would not format the output as you'd like)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With