Nested for loops and quotes

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
Jun 2, 2008
42
0
#1
I have the following alias, all one line (note the backticks):
Code:
columns = for %line in (@con) do ( ( for %field in (%line) do echos %@format[-%1, %field] `` ) %+ echo. )
I would like to define it on the fly in a batch file, and have been unable to do so, except by writing it out to a file first.

Doesn't work (inner set of backticks are lost):
Code:
columns = `for %line in (@con) do ( ( for %field in (%line) do echos %@format[-%1, %field] `` ) %+ echo. )`
Doesn't work (prints escape char, or something, as part of output):
Code:
columns = `for %line in (@con) do ( ( for %field in (%line) do echos %@format[-%1, %field] %=`%=` ) %+ echo. )`
Removing outer backticks and doubling percents results in something similar.
Code:
columns = for %%line in (@con) do ( ( for %%field in (%%line) do echos %%@format[-%%1, %%field] %=`%=` ) %%+ echo. )
This works, but is unwieldy:
Code:
text > %temp\%@name[%_batchname].alias
columns = for %line in (@con) do ( ( for %field in (%line) do echos %@format[-%1, %field] `` ) %+ echo. )
endtext
alias /r %temp\%@name[%_batchname].alias
del /q %temp\%@name[%_batchname].alias
Any suggestions?

- Josh
 
#2
joshjeppson wrote:
| I have the following alias, all one line (note the backticks):
|
| Code:
| ---------
| columns = for %line in (@con) do ( ( for %field in (%line) do echos
| %@format[-%1, %field] `` ) %+ echo. ) ---------
|
| I would like to define it on the fly in a batch file, and have been
| unable to do so, except by writing it out to a file first.

...

Did you try the "here-document" redirection?

alias /r <<-"ENDALIAS"
columns = for %line in (@con) do ( ( for %field in (%line) do echos
%@format[-%1, %field] `` ) %+ echo. )
ENDALIAS

Read the details in help topic "redirection.htm"!

N O T T E S T E D ! ! !
--
HTH, Steve
 

samintz

Scott Mintz
May 20, 2008
1,232
11
Solon, OH, USA
#3
Use SETDOS to temporarily turn off special character handling.

setdos /x-123456789
alias columns=for %line in (@con) do ( ( for %field in (%line) do echos
%@format[-%1, %field] `` ) & echo. )
setdos /x0

You'll notice that there are no backticks arround the alias definition.

-Scott

joshjeppson <> wrote on 07/15/2008 11:49:37 PM:


> I have the following alias, all one line (note the backticks):
> Code:
> columns = for %line in (@con) do ( ( for %field in (%line) do echos
> %@format[-%1, %field] `` ) %+ echo. )
> I would like to define it on the fly in a batch file, and have been
> unable to do so, except by writing it out to a file first.
>
> Doesn't work (inner set of backticks are lost):
> Code:
>
> columns = `for %line in (@con) do ( ( for %field in (%line) do echos
> %@format[-%1, %field] `` ) %+ echo. )`
> Doesn't work (prints escape char, or something, as part of output):
> Code:
>
> columns = `for %line in (@con) do ( ( for %field in (%line) do echos
> %@format[-%1, %field] %=`%=` ) %+ echo. )`
> Removing outer backticks and doubling percents results in something
similar.

> Code:
>
> columns = for %%line in (@con) do ( ( for %%field in (%%line) do
> echos %%@format[-%%1, %%field] %=`%=` ) %%+ echo. )
> This works, but is unwieldy:
> Code:
> text > %temp\%@name[%_batchname].alias
> columns = for %line in (@con) do ( ( for %field in (%line) do echos
> %@format[-%1, %field] `` ) %+ echo. )
> endtext
> alias /r %temp\%@name[%_batchname].alias
> del /q %temp\%@name[%_batchname].alias
> Any suggestions?
>
> - Josh
>
>
 
Jun 2, 2008
42
0
#4
joshjeppson wrote:
| I have the following alias, all one line (note the backticks):
|
| Code:
| ---------
| columns = for %line in (@con) do ( ( for %field in (%line) do echos
| %@format[-%1, %field] `` ) %+ echo. ) ---------
|
| I would like to define it on the fly in a batch file, and have been
| unable to do so, except by writing it out to a file first.

...

Did you try the "here-document" redirection?

alias /r <<-"ENDALIAS"
columns = for %line in (@con) do ( ( for %field in (%line) do echos
%@format[-%1, %field] `` ) %+ echo. )
ENDALIAS
This cause me a little bit of a 'Doh!' moment. It works perfectly, of course. Scott's suggestion of using setdos also works. I will be using the heredoc version, mostly because I've always disliked the underlying idea of setdos. It feels too much like cheating. :)


- Josh
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,479
44
Albuquerque, NM
prospero.unm.edu
#5
I have the following alias, all one line (note the backticks):
Code:
columns = for %line in (@con) do ( ( for %field in (%line) do echos %@format[-%1, %field] `` ) %+ echo. )
I would like to define it on the fly in a batch file, and have been unable to do so, except by writing it out to a file first.

Doesn't work (prints escape char, or something, as part of output):
Code:
columns = `for %line in (@con) do ( ( for %field in (%line) do echos %@format[-%1, %field] %=`%=` ) %+ echo. )`
Any suggestions?
Code:
alias columns = `for %line in (@con) do ( ( for %field in (%line) do echos %@format[-%1, %field] ^`^` ) %+ echo. )`
N.B. actual escape characters, not the %= pseudovariable. Variable expansion is not performed inside strong quotes. (AFAIK, no escape checking is done either, with the sole exception of escaped backquotes!)
 
Jun 2, 2008
42
0
#6
Code:
alias columns = `for %line in (@con) do ( ( for %field in (%line) do echos %@format[-%1, %field] ^`^` ) %+ echo. )`
N.B. actual escape characters, not the %= pseudovariable. Variable expansion is not performed inside strong quotes. (AFAIK, no escape checking is done either, with the sole exception of escaped backquotes!)
Long ago, I changed my escape character to something harder to type and extremely rare in usage. At one point it was alt-255, I'm not sure what it is now because I use it directly so rarely. The original reason for the change was that I used many command line regular expressions with sed, perl, etc. and the caret ^ was getting evaluated by 4NT as an escape character. This was undesired and annoying. For that reason, I have used and still use %= exclusively as my meta-metacharacter.

Perhaps I will revisit this decision and switch back to using ^ as an escape character.


- Josh
 
#7
joshjeppson wrote:
| Long ago, I changed my escape character to something harder to type
| and extremely rare in usage. At one point it was alt-255, I'm not
| sure what it is now because I use it directly so rarely. The original
| reason for the change was that I used many command line regular
| expressions with sed, perl, etc. and the caret ^ was getting
| evaluated by 4NT as an escape character. This was undesired and
| annoying. For that reason, I have used and still use %= exclusively
| as my meta-metacharacter.
|
| Perhaps I will revisit this decision and switch back to using ^ as an
| escape character.

I continued to use the default characters of 4DOS even when I switched to
4NT/TCC. There are batch programs in which I do use SETDOS to change them
temporarily. I did need to "NormalKey=ctrl-X" and redefine the key for
variable expansion(to ctrl-Y), to prevent dual use of ctrl-X.
--
Steve
 

samintz

Scott Mintz
May 20, 2008
1,232
11
Solon, OH, USA
#8
I'm not sure what the original issue was. But I just tried this and it
worked without any issues:
alias columns=`for %line in (@con) do ( ( for %field in (%line) do echos
%@format[-%1, %field] ^`^` ) & echo. )`

It worked on the command line and running in a BTM script.

-Scott

joshjeppson <> wrote on 07/16/2008 02:25:44 PM:


> I have the following alias, all one line (note the backticks):
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,479
44
Albuquerque, NM
prospero.unm.edu
#9
Long ago, I changed my escape character to something harder to type and extremely rare in usage. At one point it was alt-255, I'm not sure what it is now because I use it directly so rarely. The original reason for the change was that I used many command line regular expressions with sed, perl, etc. and the caret ^ was getting evaluated by 4NT as an escape character. This was undesired and annoying. For that reason, I have used and still use %= exclusively as my meta-metacharacter.

Perhaps I will revisit this decision and switch back to using ^ as an escape character.
Well, another strategy would be to move those troublesome characters outside of the quotes. That way we can use %= to escape them:

Code:
alias columns = `for %line in (@con) do ( ( for %field in (%line) do echos %@format[-%1, %field]` %=k%=k `) %+ echo. )`
Or just lose the internal backquotes altogether. I think the intent was to force a space onto the end of the ECHOS command line, and there are easier ways to do that:

Code:
alias columns = `for %line in (@con) do ( ( for %field in (%line) do echos %@format[-%1, %field]%=s ) %+ echo. )`
 
Jun 2, 2008
42
0
#11
Or just lose the internal backquotes altogether. I think the intent was to force a space onto the end of the ECHOS command line, and there are easier ways to do that:

Code:
alias columns = `for %line in (@con) do ( ( for %field in (%line) do echos %@format[-%1, %field]%=s ) %+ echo. )`
I like this!


- Josh
 
#12
joshjeppson wrote:
| ---Quote (Originally by Charles Dye)---
| Or just lose the internal backquotes altogether. I think the intent
| was to force a space onto the end of the ECHOS command line, and
| there are easier ways to do that:
|
|
| Code:
| ---------
| alias columns = `for %line in (@con) do ( ( for %field in (%line) do
| echos %@format[-%1, %field]%=s ) %+ echo. )`
| ---------
| ---End Quote---
|
| I like this!

Another minor simplification: JPsoft's FOR command does not require the DO
keyword.

alias columns = `for %line in (@con) ( ( for %field in (%line) echos
%@format[-%1, %field]%=s ) %+ echo. )`
--
Steve
 
Jun 2, 2008
42
0
#13
joshjeppson wrote:
Another minor simplification: JPsoft's FOR command does not require the DO keyword.

alias columns = `for %line in (@con) ( ( for %field in (%line) echos
%@format[-%1, %field]%=s ) %+ echo. )`
Yeah, I know, but I tend to prefer explicit syntax in batch files and aliases. I then use the shortcuts and simplifications at the command line.

It helps when I'm reviewing code that is months or years (or more) old.


- Josh