Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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 napster.pl 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/test.pl 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.

    BlueLines

    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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2021-12-02 10:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (18 votes). Check out past polls.

    Notices?