Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: YAFR: nl_printf() Message-ID: <11525@jpl-devvax.JPL.NASA.GOV> Date: 21 Feb 91 01:12:12 GMT References: <1991Feb11.113551.3857@robobar.co.uk> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 49 In article <1991Feb11.113551.3857@robobar.co.uk> ronald@robobar.co.uk (Ronald S H Khoo) writes: : [ Yes, line-eater, this is *Yet* another feature request ] : : I need nl_printf() -- my boss won't let me use perl without it. : (We have lots of foreign customers, and they seem to want reports : written in a language that they can understand :-( ) : : Is there any chance of adding a position independent sprintf() to perl? : Pretty please ? With a lump of sugar on top ? : : A simple %$format a'la X/Open nl_sprintf() where from : 1 to 9 specifies which parameter (plus corresponding nl_printf()) would be : adequate. I've actually hacked do_sprintf to recognise and obey this : format (diffs below) but I guess it really needs to be a separately named : nl_{s,}printf()... (which nevertheless is meant to work with non-position : specified args as well, to help with sanity...) : : Anyone got any better ideas ? Any chance of some "Official" solution ? It seems that it's fairly easy to write a subroutine to do this: #!/usr/bin/perl sub nl_sprintf { local($f) = $_[0]; local(@ix); local($ix) = 1; $f =~ s/%(\d\$)?([-+ #\d.]*[\w%])/push(@ix,$1||$ix++) if $2 ne '%';"%$2"/eg; sprintf($f, @_[@ix]); } print &nl_sprintf('%s %s %s'."\n", "one", "two", "three"); print &nl_sprintf('%3$s %2$s %1$s'."\n", "one", "two", "three"); print &nl_sprintf('%1$s %2$s %3$s'."\n", "one", "two", "three"); print &nl_sprintf('%3$s %2$s %s'."\n", "one", "two", "three"); prints one two three three two one one two three three two one I'd probably choose some other character than $ to mark position selectors, though, so that I could use double-quoted strings without backwacking every $. Maybe :. Larry