WAD handling variables with $-prefix

Aug 2, 2011
258
4
Berlin, Germany
Hi,

I'm doing some considerations for a script to create a fileage-report.
In preparation for this I'm using variables beginning with the $-sign, which should be allowed
as the help says "The environment variable names you use this way may contain any alphabetic or numeric characters, the underscore character _, and the dollar sign $"
But I have a funny behavior: when I issue the commands at the prompt, they are ok
Code:
C:\Program Files\JPSoft\TCMD13x64 >ver
 
TCC  13.04.63 x64  Windows 7 [Version 6.1.7601]
 
C:\Program Files\JPSoft\TCMD13x64 >plugin
TCC: No plugins loaded
 
C:\Program Files\JPSoft\TCMD13x64 >set    $yearsback=10
 
C:\Program Files\JPSoft\TCMD13x64 >set    $lastindex=%@eval[%$yearsback + 1]
 
C:\Program Files\JPSoft\TCMD13x64 >set    /a $nelements=%$yearsback + 2
12
 
C:\Program Files\JPSoft\TCMD13x64 >set    /a $baseyear=%_year - %$yearsback
2002
 
C:\Program Files\JPSoft\TCMD13x64 >
C:\Program Files\JPSoft\TCMD13x64 >set    $*
$baseyear=2002
$lastindex=11
$nelements=12
$yearsback=10
 
C:\Program Files\JPSoft\TCMD13x64 >
C:\Program Files\JPSoft\TCMD13x64 >unsetarray /q array
 
C:\Program Files\JPSoft\TCMD13x64 >setarray array[%$nelements]
 
C:\Program Files\JPSoft\TCMD13x64 >echo    %@arrayinfo[array,0]
  1
 
C:\Program Files\JPSoft\TCMD13x64 >echo    %@arrayinfo[array,5]
  12
and if I run them as a btm they do not work.
Code:
C:\Program Files\JPSoft\TCMD13x64 >filecount_per_year.btm
echo    on
setlocal
on      break goto end
rem    The environment variable names you use this way may contain any alphabetic or numeric characters,
rem    the underscore character _, and the dollar sign $
set    $yearsback=10
set    $lastindex=1
set    /a $nelements=yearsback + 2
set    /a $baseyear=2012 - yearsback
set    $*
$baseyear=2012
$lastindex=1
$nelements=2
$yearsback=10
unsetarray /q array
setarray array[nelements]
C:\Program Files\JPSoft\TCMD13x64\filecount_per_year.btm [16]  Usage : SETARRAY name[a[,b[,c[,c]]]] [...]
echo    1
  1
echo    0
  0
endlocal
quit

Neither "SET /A" nor "@EVAL" can handle the $variables.
Am I on the wrong track?
 
May 20, 2008
12,178
133
Syracuse, NY, USA
Hmmm!. The character is legal in variable names. Bit "%$" also has a meaning of its own. And the help doesn't say all legally-named variables may be freely de-referenced with "%". There's no problem if you de-reference them with %[]. I wonder what Rex will say.
Code:
v:\> type dollar.btm
echo on
set    $yearsback=10
set    $lastindex=%@eval[%[$yearsback] + 1]
set    /a $nelements=%[$yearsback] + 2
set    /a $baseyear=%_year - %[$yearsback]
set $*
unsetarray /q array
setarray array[%[$nelements]]
echo    %@arrayinfo[array,0]
echo    %@arrayinfo[array,5]
 
v:\> dollar.btm
set    $yearsback=10
set    $lastindex=11
set    /a $nelements=10 + 2
set    /a $baseyear=2012 - 10
set $*
$baseyear=2002
$lastindex=11
$nelements=12
$yearsback=10
unsetarray /q array
setarray array[12]
echo    1
  1
echo    12
  12
 
Aug 2, 2011
258
4
Berlin, Germany
Vince, %[] is a cool idea, but makes it unnecessarry complicated.
Then I would abdicate the dollar-sign.
Perhaps I should use the euro-sign € ;)
 
May 20, 2008
12,178
133
Syracuse, NY, USA
Vince, %[] is a cool idea, but makes it unnecessarry complicated.
Then I would abdicate the dollar-sign.
Perhaps I should use the euro-sign € ;)
Is the Euro symbol readily accessible (wouldn't know here in the states). Do you need a prefix at all?

P.S., The "Frank" image is a bit unnerving but it brings back fond memories and has prompted me to listen (after a long time) to some of the most creative music I can think of.
 
May 20, 2008
12,178
133
Syracuse, NY, USA
There are a few other places where the %[] notation is needed ... IIRC, in "DO x" and "FOR %x" routines, because one-letter variable names have some preference.
 
Aug 2, 2011
258
4
Berlin, Germany
Is the Euro symbol readily accessible (wouldn't know here in the states).
I can access it on my german keyboard with AltGr-E.
Do you need a prefix at all?
No, it's just for having them all togehter with a "set $*". I thought it could be convenient for my little "project" because I fear I will have to check the variables very often;)

P.S., The "Frank" image is a bit unnerving but it brings back fond memories and has prompted me to listen (after a long time) to some of the most creative music I can think of.
I'm very happy I brought FZ back in mind. I am/was a really big fan since I was ~16 years old.
But why is the picture unnerving? Do you think the other forum members could be nerved, too?
Please let me know.
 
May 20, 2008
12,178
133
Syracuse, NY, USA
I'm very happy I brought FZ back in mind. I am/was a really big fan since I was ~16 years old.
But why is the picture unnerving? Do you think the other forum members could be nerved, too?
Please let me know.
Basically because he's no longer with us and I figure if that were not the case **someone** would, today, be making new music that I like. I'm not offended by the image; it just makes me think.
 
May 20, 2008
12,178
133
Syracuse, NY, USA
And just to jog a few more memories, last week, Donovan (as in Donovan Leitch) performed at a jazz festival here in Syracuse, NY.
 
Aug 2, 2011
258
4
Berlin, Germany
I've choosen the picture as a kind of hommage. And sometimes I get the blues, too, if I think of former times :cool:
 
Aug 2, 2011
258
4
Berlin, Germany
what do you think of this version?
Code:
echo    on
setlocal
on      break goto end
 
set    _$yearsback=10
set    _$lastindex=%@eval[%_$yearsback + 1]
set    /a _$nelements=%_$yearsback + 2
set    /a _$baseyear=%_year - %_$yearsback
 
set    _$*
 
unsetarray /q _$array
setarray _$array[%_$nelements]
echo    %@arrayinfo[_$array,0]
echo    %@arrayinfo[_$array,5]
 
endlocal
quit
which doesn't run into the %$ trap:
Code:
echo    on
setlocal
on      break goto end
set    _$yearsback=10
set    _$lastindex=11
set    /a _$nelements=10 + 2
set    /a _$baseyear=2012 - 10
set    _$*
_$baseyear=2002
_$lastindex=11
_$nelements=12
_$yearsback=10
unsetarray /q _$array
setarray _$array[12]
echo    1
  1
echo    12
  12
endlocal
quit
 

rconn

Administrator
Staff member
May 14, 2008
12,557
167
Hi,

I'm doing some considerations for a script to create a fileage-report.
In preparation for this I'm using variables beginning with the $-sign, which should be allowed
as the help says "The environment variable names you use this way may contain any alphabetic or numeric characters, the underscore character _, and the dollar sign $"
But I have a funny behavior: when I issue the commands at the prompt, they are ok

WAD -- see the help for "batch file parameters". %$ has a different meaning in batch files.
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,689
106
Albuquerque, NM
prospero.unm.edu
I have a vague memory that the help file once strongly recommended beginning variable names with a letter. Now I can't find it. Was the suggestion removed, or is this just a Fig Newton of my imagination?
 

rconn

Administrator
Staff member
May 14, 2008
12,557
167
I don't know that it was ever in the documentation, but it is definitely a good idea. Variable names beginning with a '_' are assumed to be internal variables (though you can override them with an environment variable, after a wasted search through the internal list), and variables beginning with a number or '$' are treated as batch (or alias) parameters.

If users are suffering from an overwhelming compulsion to use otherwise invalid characters in variable names, they can always use the %[] syntax.
 
Aug 2, 2011
258
4
Berlin, Germany
I don't know that it was ever in the documentation, but it is definitely a good idea. Variable names beginning with a '_' are assumed to be internal variables (though you can override them with an environment variable, after a wasted search through the internal list), and variables beginning with a number or '$' are treated as batch (or alias) parameters.

If users are suffering from an overwhelming compulsion to use otherwise invalid characters in variable names, they can always use the %[] syntax.
Yet a little bit stressed? Not very politely :(
I don't know why I ran into this trap. Normally I do not use any prefixes in my variables.
And I said
Am I on the wrong track?
 

Similar threads