Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printf in a subroutine

Status
Not open for further replies.

columb

IS-IT--Management
Feb 5, 2004
1,231
EU
My code looks something like this
Code:
sub print_line
  {
  my ( $format, $arref ) = @_;
  foreach ( 0..15 )
    { printf $format, $$arref[$_]; }
  print "\n";
  }

print_line '|  %2.2s  ', \@data;
but I'm getting
Code:
Invalid conversion in printf: "%
I've tried various formats but I'm out of ideas. Any pointers would be gratefully received.

Thanks

Ceci n'est pas une signature
Columb Healy
 
printf needs format codes taht start with a % sign. I can't see any in your program.

see:


_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Cracked it - what worked was
Code:
sub print_line
  {
  my ( $format, $arref ) = @_;
  foreach ( 0..15 )
    { printf "$format", $$arref[$_]; }
  print "\n";
  }

print_line '|  %2.2s  ', \@data;

Ceci n'est pas une signature
Columb Healy
 
There is no practical distinction between [tt]$format[/tt] and [tt]"$format"[/tt] and both the code fragments you posted produce identical results. I think you had a problem with your test procedure.

Yours,

fish

["]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life was going to be spent in finding mistakes in my own programs.["]
--Maurice Wilkes, 1949
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top