sprintf is stripping leading zeros from symbols (not ints)

May 20, 2010 at 3:22pm

sprintf is stripping leading zeros from symbols (not ints)

I know how to tell sprintf to pad an int with leading zeros, but this problem is different: I’m trying to fill a jit.cellblock with filenames, but the names often begin with numbers. I’m using sprintf to generate a message like
set 0 1 filename
The filename might actually be “01 Foo.mp3″ and sprintf insists on changing that to
“1 Foo.mp3″. How do I stop it? Simple test patch below:

– Pasted Max Patch, click to expand. –
#50472
May 20, 2010 at 3:30pm

filename – tosymbol – sprintf ?

“01 foo.mp3″ is a list with two arguments of type int and symbol …

#181070
May 20, 2010 at 4:30pm

yup, there’s a tosymbol in the patch already. That’s what’s so confusing.

#181071
May 20, 2010 at 7:08pm

The problem is that sprintf is making a list to send to cellblock, and leading zeros are not accepted in a list. You can’t use symout in sprintf because then jit.cellblock won’t understand it. Here’s a sprintf-free solution.

– Pasted Max Patch, click to expand. –
#181072
May 20, 2010 at 7:09pm

the output of sprintf will be a list of symbols and numbers, so that number becomes a number again. You can use the symout attribute to output the whole deal as a symbol, but in your then you would have to prepend the rest of the string contents to get it to do what you want it to do, which is a hassle and probably not that efficient.

In this case, I’d actually just use a pak object and avoid sprintf altogether:

– Pasted Max Patch, click to expand. –
#181073
May 20, 2010 at 7:10pm

and/or what Chris said :)

-Ben

#181074
May 20, 2010 at 7:13pm

Similar solutions, seconds apart. The problem with the pak solution is that it will redundantly set the cellblock cell. (I’m on an efficiency kick lately, trying to wring the last bit of performance out of my aging MacBook Pro)

#181075
May 20, 2010 at 7:29pm

ah whoops, good catch, lazy patching on my part. A [t b i] and a pack [set 0 i s] would work too:

– Pasted Max Patch, click to expand. –
#181076
May 21, 2010 at 3:47am

Wonderful. Thanks to you both. (I have always avoided pack whenever symbols are involved but now I’m enlightened.)

#181077

You must be logged in to reply to this topic.