1.

Solve : for loop deliminator question?

Answer» <html><body><p>can i have a for loop so every character is assigned a diffrent %%X<br/>for ex<br/>123456 becomes %%a=1 %%<a href="https://interviewquestions.tuteehub.com/tag/b-236590" style="font-weight:bold;" target="_blank" title="Click to know more about B">B</a>=2 %%c=3 %%d=4 %%a=e %%f=6No you can't. The FOR command cannot separate a <a href="https://interviewquestions.tuteehub.com/tag/string-11290" style="font-weight:bold;" target="_blank" title="Click to know more about STRING">STRING</a> into characters. If you let <a href="https://interviewquestions.tuteehub.com/tag/us-243201" style="font-weight:bold;" target="_blank" title="Click to know more about US">US</a> know what you are <a href="https://interviewquestions.tuteehub.com/tag/trying-3234509" style="font-weight:bold;" target="_blank" title="Click to know more about TRYING">TRYING</a> to do somebody might be able to suggest a method, or were you just curious?<br/>just curiousYou have to give the FOR command one or more "delimiters", to use when deciding when one token ends and the next one begins. Delimiters can bethe line start and end, one or more characters.<br/><br/>You could have "delims=." and then for could read a.b.c.d.e etc and separate them out into tokens.<br/>You can insert the delimiters with one <strong>for</strong> statement and then parse the new string with another <strong>for</strong> statement. The question is why? Tokens as opposed to variables only live as long as the for loop. Seems like a lot of work for so little payoff.<br/><br/> Code: <a>[Select]</a>echo off<br/><br/>setlocal enabledelayedexpansion<br/>set str=123456<br/><br/>for /l %%v in (0,1,5) do (<br/>  set string=!string!!str:~%%v,1!-<br/>)  <br/> <br/>for /f "tokens=1-6 delims=-" %%a in ("!string!") do (<br/>  echo %%a %%b %%c %%d %%e %%f<br/>)  <br/><br/>I used a <a href="https://interviewquestions.tuteehub.com/tag/dash-434695" style="font-weight:bold;" target="_blank" title="Click to know more about DASH">DASH</a> (-) as the delimiter, but it is arbitrary. Just don't use any character that has special meaning in batch language.<br/><br/>Good luck.  You would need to know the string length in advance, and I am not sure what advantage putting the characters into loop metavariables would have over the ordinary sltring slicing methods. In essence the code above takes about 6 lines to slice the string into 6 characters just so you can... slice it into 6 characters.<br/> Quote from: mat123 on June 26, 2010, 08:32:56 AM</p><blockquote>can i have a for loop so every character is assigned a diffrent %%X<br/>for ex<br/>123456 becomes %%a=1 %%b=2 %%c=3 %%d=4 %%a=e %%f=6<br/></blockquote> <br/>as i have always suggested, if you want to do various programming tasks easily and productively, such as this one you mentioned, use a good programming language.<br/><br/>Example in Python<br/> Code: <a>[Select]</a># python<br/>Python 2.6.2 (r262:71600, Aug 21 2009)<br/>Type "help", "copyright", "credits" or "license" for more information.<br/>&gt;&gt;&gt; my_string="abcdefg"<br/>&gt;&gt;&gt; list(my_string)<br/>['a', 'b', 'c', 'd', 'e', 'f', 'g']<br/>&gt;&gt;&gt;<br/><br/>Now you have all your individual characters separated. Easy isn't it?<br/></body></html>


Discussion

No Comment Found