Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accumulating macros with embedded spaces and quotes

    I am trying to write a loop that operates on file names with embedded spaces. I am struggling (not for the first time!) with macros and compound double quotes.

    How can I accumulate files names in a local macro over the loop, using quotes the handle the embedded spaces? How can I then operate on the resulting macros to extract each individual filename, including the spaces?

    Here's a (non-)working example:

    Code:
    foreach table in "A table" "Another table" {
        local alltables  `"`alltables'"' `" "`table'" "' 
        }
    * this looks right, because each table name is enclosed in quotes:
    di `"`alltables'"'
    * this doesn't look right, because quotes are not handled properly:
    foreach table of local alltables {
        di "table: `table'"
        }

  • #2
    Michael,

    I don't think that display works exactly like other commands so I'm not sure that trying to get this example to work how you want is going to help you perform whatever procedures you want to run on the files. Perhaps you could explain and show what you are actually trying to do. For example, why bother to accumulate the names "A table" and "Another table" in a local macro in a loop when you could just just declare the macro to be the list of names...
    Code:
    local all tables "A table" "Another table"
    There are a number of examples on Statalist of working with files and embedded spaces are a common element of the posts. Here's an example that I use for some student assignments. The course management software puts an extended prefix on the files when students submit them so, after a batch download of all the submitted files into a particular directory, I want to remove the prefix by renaming the files. The file names have embedded spaces because of how I name the directories and because that's how I've instructed the students to name the files. Perhaps you can use this as an example to solve your problem.
    Code:
    local lab Lab 01
    local dir "/Users/lde/box/405/2020 Fall/Labs/`lab'"
    local files: dir "`dir'" files "*.*"
    foreach old in `files' {
        local new = substr("`old'",1+strrpos("`old'","_"),.)
        !mv "`dir'/`old'" "`dir'/`new'"
    }
    Lance

    Comment


    • #3
      Lance is correct that display is not revealing what you expect. The macro list command is what you need for debugging. Below I repeat your example with macro list showing where things go awry, and the provide a revised version of your code that seems to do what you want.
      Code:
      . foreach table in "A table" "Another table" {
        2.     local alltables  `"`alltables'"' `" "`table'" "' 
        3.     }
      
      . macro list _alltables
      _alltables:     "' `" "A table" "' `" "Another table"
      
      . foreach table of local alltables {
        2.     macro list _table
        3.     }
      _table:         ' `
      _table:         A table
      _table:         ' `
      _table:         Another table
      
      . 
      . local alltables
      
      . foreach table in "A table" "Another table" {
        2.     local alltables `" `alltables' "`table'" "'
        3.     }
      
      . macro list _alltables
      _alltables:      "A table" "Another table"
      
      . foreach table of local alltables {
        2.     macro list _table
        3.     }
      _table:         A table
      _table:         Another table
      
      .

      Comment


      • #4
        Thanks to both for teaching me about macro list, and to William for the corrected code. Now for the teach-me-to-fish question. Looking at
        Code:
        local alltables `" `alltables' "`table'" "'
        it looks like the outer `" "' tells Stata to preserve all the quote marks, including those embedded in the existing macro `alltables', and the inner " " tells it to add quotes around the new element being added to the macro.

        Is that right? It seems to make sense - more sense that randomly adding single and double quotes to the code - my usual strategy :-)

        Comment


        • #5
          it looks like the outer `" "' tells Stata to preserve all the quote marks, including those embedded in the existing macro `alltables', and the inner " " tells it to add quotes around the new element being added to the macro.
          Absolutely correct. If you are interested in further details, the link below points to a discussion on Statalist in which the rules were explained in a way that was finally clear to me.

          https://www.statalist.org/forums/for...-list-question

          Comment

          Working...
          X