Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

printf confusion

by dusk (Friar)
on Feb 21, 2001 at 07:17 UTC ( #59837=perlquestion: print w/replies, xml ) Need Help??

dusk has asked for the wisdom of the Perl Monks concerning the following question:

I am having a bit of trouble with a Napster client I am writing (using deprecated's MP3::Napster module)

The client included with MP3::Napster uses a similar sub to the one below to recieve a list of names in the channel:

109 sub names 110 { 111 unless ($nap_object->channel){ 112 print qq-No current channel\n-; 113 return; 114 } 115 foreach ($nap_object->users) { 116 printf "\t([ %-20s ]) (%-9s)(%4d)\n",$_,$_->s +haring,$_->link; 117 } 118 }
However, this returns: Argument "DSL" isn't numeric in printf at perl/duskster/ line 116. I was informed that printf is unecessary in perl, and that there are far better ways to do it. Any suggestions/fixes will be greatly appreciated :)

Replies are listed 'Best First'.
Re: printf confusion
by AgentM (Curate) on Feb 21, 2001 at 07:23 UTC
    If you don't want to use printf, then simply use print. The current printf will give you a nice tab and lined up stuff, but otherwise, if you don't know how to use it, you will simply get confused. The error has something to do with $nap_object->users->link- which is not a number. If this is a string (I'm not familiar with this module) and you wish to print it, then switch that last 'd' in your printf line to 's'. 'd' specifies a digit and that variable doesn't seem to be a number, hence the error.
    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
Re: printf confusion
by BlueLines (Hermit) on Feb 21, 2001 at 07:25 UTC
    This is fine. You just need to do sanity checking on $_->link, which (in theory) usually is a number (14.4, 28.8), but can also be a string (DSL, CABLE, Unknown). Change the %4d to %-6s for instance, and everything will work fine.


    Disclaimer: This post may contain inaccurate information, be habit forming, cause atomic warfare between peaceful countries, speed up male pattern baldness, interfere with your cable reception, exile you from certain third world countries, ruin your marriage, and generally spoil your day. No batteries included, no strings attached, your mileage may vary.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://59837]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2022-12-09 19:27 GMT
Find Nodes?
    Voting Booth?

    No recent polls found