Shorten code lines in a For Loop Question

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Shorten code lines in a For Loop Question

#1 Post by Jer » 18 Aug 2016 00:09

My For loop parses up to 20 values from a string, and I am looking for a shorter way rather than
having 20 lines of code inside a For loop to do this. My nested For loop below does not work.
Is this shorter solution possible?
In the example code the results would be to assign new variables %a%=32, %b%=65, etc.

Code: Select all

@Echo Off
setlocal EnableDelayedExpansion

Set "str=32 65 66 67 68 69 70 71 72 73 74 75 76 77 78 78 80 81 82 32"

For /F "tokens=1-20 delims= " %%a In ("%str%") Do (
   echo test vars:
   echo a: %%a
   echo b: %%b
   echo c: %%c
   For %%x In (a b c d e f g h i j k l m n o p q r s t) Do (
      Set "%%x=!%%x!" & echo ...x var name: %%x --- value: !%%x!
))

endlocal & exit /b


Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Shorten code lines in a For Loop Question

#2 Post by Aacini » 18 Aug 2016 00:34

Code: Select all

@Echo Off
setlocal EnableDelayedExpansion

Set "str=32 65 66 67 68 69 70 71 72 73 74 75 76 77 78 78 80 81 82 32"
Set "vars=a b c d e f g h i j k l m n o p q r s t"

For %%a In (%str%) Do (
   For /F "tokens=1*" %%x In ("!vars!") Do (
      Set "%%x=%%a" & echo ...x var name: %%x --- value: !%%x!
      Set "vars=%%y"
   )
)

endlocal & exit /b

Output:

Code: Select all

...x var name: a --- value: 32
...x var name: b --- value: 65
...x var name: c --- value: 66
...x var name: d --- value: 67
...x var name: e --- value: 68
...x var name: f --- value: 69
...x var name: g --- value: 70
...x var name: h --- value: 71
...x var name: i --- value: 72
...x var name: j --- value: 73
...x var name: k --- value: 74
...x var name: l --- value: 75
...x var name: m --- value: 76
...x var name: n --- value: 77
...x var name: o --- value: 78
...x var name: p --- value: 78
...x var name: q --- value: 80
...x var name: r --- value: 81
...x var name: s --- value: 82
...x var name: t --- value: 32

Antonio

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: Shorten code lines in a For Loop Question

#3 Post by Jer » 18 Aug 2016 09:47

Thanks Antonio for that solution that reduces the number of code lines in my For loop.
It politely handles any number of tokens up to the limit of 20. Very educational!
Jerry

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Shorten code lines in a For Loop Question

#4 Post by Aacini » 18 Aug 2016 11:40

I don't know how you plan to use the individual variables created; however, I suggest you to use an array instead:

Code: Select all

@Echo Off
setlocal EnableDelayedExpansion

:nextInput
echo/
echo Enter a series of numbers separated by space
Set "str="
Set /P str=
if not defined Str goto :EOF

set n=0
For %%a In (%str%) Do (
   set /A n+=1
   set "num[!n!]=%%a"
)

echo Numbers read: %n%
for /L %%i in (1,1,%n%) do echo %%i- !num[%%i]!
goto nextInput

Antonio

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: Shorten code lines in a For Loop Question

#5 Post by Jer » 18 Aug 2016 12:45

The code will be part of my revised and enhanced FrameTool/ColorShow.exe project,
where you can fill a frame with an ascii character by enterering an ascii code,
only the next release will accept up to 20 ascii codes as well as first-column and
last-column codes for this frametool option.

Each value in a colon-delimited string of ascii codes will be examined for being numeric and in
the range 1-255, adjusted to 32 if non-numeric or out of range, and put back in a string that
has as many tokens as the width switch designates, plus 2 if right column and left column codes
are included. So part of the frametool command-line entry might be /w30 /h6 /x17:16+32:32
and you get a frame of 6 lines with chars #17 & #16 displayed 15 times on each line and with
added blank left and right columns.

A frivolous effort, but if I can do it, why not?
Last edited by Jer on 22 Aug 2016 12:40, edited 1 time in total.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Shorten code lines in a For Loop Question

#6 Post by Aacini » 18 Aug 2016 16:48

Mmmm... Did you read these parts in the last update of my auxiliary programs?

Aacini wrote:The executable files are now on my DropBox site as public files...

Before upload the files on that site I reviewed the programs of this thread and updated some of they...

Modifications to existing programs:

Code: Select all

ColorShow.exe           Now allows to fill the field of a variable with a given character.

Add some new programs:

Code: Select all

ColorBox.exe            Show a box in text mode using line-drawing characters.


ColorBox.txt:

Code: Select all

Show a box in text mode using line-drawing characters.

ColorBox [/BF] left top right bottom [/D] [ac1 ... ac8 [ac9]]

   /BF       Color for the box.
   l t r b   Coordinates of left,top and right,bottom box corners.
   /D        Use double-lines in the frame.
   ac1...    Use these Ascii codes in the frame.

If just the box coordinates are given, a frame is shown with the same color
of the initial position and single lines.

If /BF switch is given, it specify the color for the frame; see: COLOR /?

if /D switch is given, the frame is shown with double lines.

Different characters for the box may be specified via AC1..AC9 Ascii codes:
- AC1..AC8 codes are used to show the frame corners/sides in clockwise order.
- If AC9 is given, the inside of the frame is filled with this character.
- If just AC1..AC4 codes are given, they specify characters for frame corners;
  for the sides, the standard single or double lines characters are used.

Standard AC1..AC8 codes for single lines: 218 196 191 179 217 196 192 179
Standard AC1..AC8 codes for double lines: 201 205 187 186 188 205 200 186

After the box is shown, the cursor is returned to its original position.

Perhaps these modifications allows you to create your FrameTool project in a simpler way...

Antonio

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: Shorten code lines in a For Loop Question

#7 Post by Jer » 18 Aug 2016 19:26

I'll take a look at your updates. As you are probably aware of from the frametool thread,
two borders can surround the framed text. The next version allows widening the vertical
part of the 1st border to 1 or 2 additional columns. A separate horizontal line can be added
at the top and/or bottom with assigned characters and colors. The grid of frames option
will no longer be subject to variable size limitations. If the colorshow parameters are likely
the exceed 8,000 in length, the parameters will be divided into 2 or 3 sets, as needed,
and colorshow will be called 2 or 3 times for the larger grids.

A unique 1st border will be added. When I realized that character #127 did not display correctly
in the font I chose (Consolas), except if it was preceded by another character, I decided to
use this solution for this char. in a border: 255 127 32 (for verticals) or 255 127*30 32 (for horiz)
Then, as long as I'm was doing that, I added the option of replacing char. #127 with any other
character for the unique border style. The border char. replacement parameter will be like
this: /b4e 127:178

In the current version you have the written batch file to display your frame, and not
be dependent on frametool.bat with all its processing to construct the frame.

Thanks again Antonio for informing me of the colorshow.exe updates.
As long as colorshow works the same way as the version I have, I will
be a happy camper :)

edit:
some trivia just discovered. colorshow /1b 127 65 66 67 displays a question mark in a box and ABC, but
colorshow /1b 127 65 66 67 255 displays ascii code #127 correctly, a square with a peak on top, and also
any ascii code in the range 128-255 included anywhere in the set of codes seems to "befriend" ascii #127
so that it shows correctly. If there is an ascii #32 in the set that includes #127, one of them could be replaced
by code #255, which displays a blank space.
colorshow /e4 "They are happy campers who know how to quickly assemble their" 255 127"." 13 10
Last edited by Jer on 20 Aug 2016 20:17, edited 1 time in total.

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: Shorten code lines in a For Loop Question

#8 Post by Jer » 18 Aug 2016 23:18

I am not familiar with dropbox. I did set up an account.
When I click on "this link" at your updates thread, I get a white
screen and the address bar has this site's web address.
How do I get to your files on dropbox?

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: Shorten code lines in a For Loop Question

#9 Post by Jer » 19 Aug 2016 18:06

Figured it out.
Using the dropbox link requires right mouse-click, as opposed to left click.
:oops:

Post Reply