Please explain how the $ works

The code below works. I got it from someone else. It searches through each of the waves in the list of Metwaves and replaces the "nan" with a "-999". What I don't understand is what the dollar sign does, how it works and more importantly when to or not or use it. The values in the original wave are changed but there is no direct mechanism that reassigns the changed values in "tempwave" back to the original wave. Any clarification/explanation is appreciated. Thanks,

ssmith

Make/O/T Metwaves = {"temp_2", "temp_10", "temp_50", "temp_85", "speed_10", "speed_50", "speed_85","gust_10", "gust_50", "gust_85", "dir_10", "dir_50",
"dir_85", "deviation_10", "deviation_50", "deviation_85", "rh", "baro", "solar", "precip"}
Variable selects

FOR(selects = 0; selects < (numpnts(Metwaves)); selects += 1)
Wave tempwave = $Metwaves[selects]
tempwave = (numtype(tempwave) == 2) ? -999 : tempwave[p]
ENDFOR
ssmith911 wrote:
The code below works. I got it from someone else. It searches through each of the waves in the list of Metwaves and replaces the "nan" with a "-999". What I don't understand is what the dollar sign does, how it works and more importantly when to or not or use it


$ is used when a command requires a name but you have only a String.

It converts the contents of the string into a name.

Look at the command syntax for WAVE:

WAVE [/C][/T][/Z] localName [= pathToWave ][, localName1 [= pathToWave1 ]]...

pathToWave isn't shown as pathToWaveStr, just pathToWave.

Without the Str ending, that's a hint that the parameter is a name, like for the Make command:

Make [flags ] waveName  [, waveName ]...

Thus code like this won't work:
String theWaveName = "dest"
Make/O theWaveName  // "Inconsistent type for a wave reference" error: expected the name of a wave (and got the name of a string)

But this will:
String theWaveName = "dest"
Make/O $theWaveName  // $ "looks inside" the string for the name. The created wave is named dest.


For more about this, use this command:

DisplayHelpTopic "Converting a String into a Reference Using $"

--Jim Prouty
Software Engineer, WaveMetrics, Inc.
ssmith911 wrote:
The code below works. I got it from someone else. It searches through each of the waves in the list of Metwaves and replaces the "nan" with a "-999". ... The values in the original wave are changed but there is no direct mechanism that reassigns the changed values in "tempwave" back to the original wave. Any clarification/explanation is appreciated.


I've added comments to the code:

    // Make a wave of strings containing the names of waves
    Make/O/T Metwaves = {"temp_2", "temp_10", "temp_50", "temp_85", "speed_10", "speed_50", "speed_85","gust_10", "gust_50", "gust_85", "dir_10", "dir_50",
        "dir_85", "deviation_10", "deviation_50", "deviation_85", "rh", "baro", "solar", "precip"}
   
    Variable selects       

        FOR(selects = 0; selects < (numpnts(Metwaves)); selects += 1)
          // Make a reference to one of the waves using the name in the wave of strings.
          Wave tempwave = $Metwaves[selects] // reference to wave of the given name. $ converts string to name
          // Now tempwave is a "pointer" or "reference" to the global wave.
          // tempwave is NOT a local wave
          // For each point (row) p in that wave, assign back to tempwave[p] either its current value, or -999 if a NaN
              tempwave = (numtype(tempwave)  == 2) ? -999 : tempwave[p]
          // The previous line is clearer if rewritten as:
              // tempwave = (numtype(tempwave[p])  == 2) ? -999 : tempwave[p]
        ENDFOR


--Jim Prouty
Software Engineer, WaveMetrics, Inc.