moin Andy,
This is quite easy using [string2any] and [any2string] together with
pd's [list] operations. see attached example patch. I'm sure the same
basic idea can be used to pad [str] strings as well...
marmosets,
Bryan
On 2007-12-16 20:54:20, "Andy Graybeal" andy.graybeal@casanueva.com
appears to have written:
greetings
i'd like to pad a string... say for instance i want to pad the string
"andy" and "ian" to be able to have a total of 10 characters, and the
padding character will be an underscore. so the end result would add 6
underscores to the string "andy" and seven underscores to the string
"ian":
"andy______"
"ian_______"
i poked around through extended and did a search on the list archives for
"character pad" and "string pad", nothing came up in the archives, and the
only things i found in extended are mrpeach's [str] help examples, and
moocow's [string2any], [any2string] help examples. it looks like
mrpeach's stuff is the closest to what i want, but i don't think it's what
i want. after tofofmtl introduced me to pdmtl, i expected there would be
some string handling in to coincide with the list stuff they have too, but
nothing there.
thanks,
andy
--
Bryan Jurish "There is *always* one more bug."
jurish@ling.uni-potsdam.de -Lubarsky's Law of Cybernetic Entomology
#N canvas 688 480 470 300 10;
#X msg 206 101 _;
#X msg 236 101 .;
#X msg 23 99 foo;
#X msg 55 99 bonk;
#X msg 266 101 x;
#X floatatom 350 104 5 0 0 0 - - -;
#N canvas 344 148 570 314 pad_string 0;
#X obj 48 16 inlet;
#X obj 48 76 any2string 0 -1;
#X obj 48 131 list split 10;
#X obj 48 221 outlet;
#X obj 48 155 string2any 0 -1;
#X obj 210 12 inlet;
#X obj 48 105 list append 95;
#X text 59 37 string;
#X text 219 33 default;
#X obj 210 75 t l l;
#X obj 240 101 list length;
#X text 123 191 Since we have a default string-list of the desired
length in the right inlet , all we have to do is append it to the
stringified message on the left inlet and discard anything beyond the
padding limit. On the way out , we convert the string-list back to
pd-messagehood with string2any.;
#X connect 0 0 1 0;
#X connect 1 0 6 0;
#X connect 2 0 4 0;
#X connect 4 0 3 0;
#X connect 5 0 9 0;
#X connect 6 0 2 0;
#X connect 9 0 6 1;
#X connect 9 1 10 0;
#X connect 10 0 2 1;
#X restore 100 182 pd pad_string;
#N canvas 76 80 482 372 default_string 0;
#X obj 40 18 inlet;
#X obj 222 18 inlet;
#X obj 222 264 outlet;
#X msg 222 239 95 95 95 95 95 95 95 95 95 95;
#X obj 222 149 until;
#X msg 329 217 set;
#X obj 222 126 t f b;
#X obj 40 82 t b f;
#X msg 222 217 add2 $1 , bang;
#X obj 222 106 f 10;
#X obj 222 194 f 95;
#X obj 40 60 any2string 0 -1;
#X text 83 19 pad_char;
#X text 265 19 default_length;
#X text 31 295 Here , we use "add2" and "set" messages to a msgbox
to build a default string of the desired length. This could be done
more efficiently , but it ought to suffice to illustrate the principle.
;
#X connect 0 0 11 0;
#X connect 1 0 9 0;
#X connect 3 0 2 0;
#X connect 4 0 10 0;
#X connect 5 0 3 0;
#X connect 6 0 4 0;
#X connect 6 1 5 0;
#X connect 7 0 9 0;
#X connect 7 1 10 1;
#X connect 8 0 3 0;
#X connect 9 0 6 0;
#X connect 10 0 8 0;
#X connect 11 0 7 0;
#X restore 236 138 pd default_string;
#X text 215 83 padding char;
#X text 355 87 pad to length;
#X msg 93 99 grok it;
#X obj 206 18 loadbang;
#X obj 100 207 print padded;
#X msg 297 101 ?;
#X text 25 81 raw message to pad;
#X msg 350 55 10;
#X connect 0 0 7 0;
#X connect 1 0 7 0;
#X connect 2 0 6 0;
#X connect 3 0 6 0;
#X connect 4 0 7 0;
#X connect 5 0 7 1;
#X connect 6 0 12 0;
#X connect 7 0 6 1;
#X connect 10 0 6 0;
#X connect 11 0 0 0;
#X connect 11 0 15 0;
#X connect 13 0 7 0;
#X connect 15 0 5 0;