1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Nested for loops and quotes

Discussion in 'Support' started by joshjeppson, Jul 15, 2008.

  1. joshjeppson

    Joined:
    Jun 2, 2008
    Messages:
    42
    Likes Received:
    0
    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. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    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

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

    N O T T E S T E D ! ! !
    --
    HTH, Steve
     
  3. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,179
    Likes Received:
    11
    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:


    similar.

     
  4. joshjeppson

    Joined:
    Jun 2, 2008
    Messages:
    42
    Likes Received:
    0
    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
     
  5. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,280
    Likes Received:
    38
    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!)
     
  6. joshjeppson

    Joined:
    Jun 2, 2008
    Messages:
    42
    Likes Received:
    0
    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. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    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
     
  8. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,179
    Likes Received:
    11
    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:


     
  9. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,280
    Likes Received:
    38
    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. )`
    
     
  10. joshjeppson

    Joined:
    Jun 2, 2008
    Messages:
    42
    Likes Received:
    0
    It does indeed, if your escape character is a caret (^).


    - Josh
     
  11. joshjeppson

    Joined:
    Jun 2, 2008
    Messages:
    42
    Likes Received:
    0
    I like this!


    - Josh
     
  12. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    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
     
  13. joshjeppson

    Joined:
    Jun 2, 2008
    Messages:
    42
    Likes Received:
    0
    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
     

Share This Page