Page 1 of 1 1
Topic Options
#79484 - 2003-02-06 05:07 AM Quoting Strings ??
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
There was a discussion about quoting strings - I can't find it right now - and I came across something tonight that may merit further discussion...

If I say:
code:
 
$X = "first"
? "X=$X, Y=$Y" ?

I get "X=First, Y=$Y" - which I would kind of expect, since $Y is undeclared and inside of quotes. However, if I rewrite it as:
code:
 
$X = "first"
? "X=$X, Y=" + $Y ?

I would expect the interpreter to recognize $Y as an uninitialized variable and output Null, but it outputs "$Y" again.

In this example, it isn't a big deal, but I have a few scripts that I was troubleshooting tonight where I execute a command via the Shell statement. I wanted to optionally write the results to a log file. I tried
code:
 
If $OPTION = "-l"
$LOG = "2>&1>> C:\TEMP\my.log"
EndIf
$CMD = "%COMSPEC% /c someprog.exe $LOG"
shell $CMD

I expected that if $LOG was undefined, it would be NULL and the command output would appear as expected. If $LOG was defined, it would receive the results via the redirection. However, when $LOG is undefined, the command complains that "$LOG" is an unknown parameter.

Of course, this is easily fixed by first declaring $LOG to be null. Maybe there should be consideration for a defined "print" command that would treat all "$XXX" format strings as vars, initialized or not. ???

Glenn

PS - yes, I know that good coding requires that vars be declared, but that's why I script [Wink]
_________________________
Actually I am a Rocket Scientist! \:D

Top
#79485 - 2003-02-06 07:06 AM Re: Quoting Strings ??
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
glenn, I hear you and I'm with you on this one.
100%

not sure that this has to do with the unquoted unknown keywords stuff I posted earlier but the symptoms are pretty much the same. [Frown]

that really is not expected nor it's wanted.
_________________________
!

download KiXnet

Top
#79486 - 2003-02-06 04:37 PM Re: Quoting Strings ??
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
See the SETOPTION function in the KiXtart Manual. A combination of NoVarsInStrings and Explicit will solve most of these issues.
_________________________
There are two types of vessels, submarines and targets.

Top
#79487 - 2003-02-06 05:01 PM Re: Quoting Strings ??
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
There is one minor gotcha to be aware of.

Supposing you have a script which can take an optional parameter on the command line. In this case, you cannot set "Explicit" on.

The problem is that if the variable is not defined on the command line (it is optional after all) then you cannot reference it at all. You cannot check to see if it is undefined, as the first time that you attempt to reference it you will get an error.

You cannot DIM the variable in the script, as that will destroy the value passed on the command line.

This means that you must always pass the variable on the command line, even if you set it to "" (which is of course a number, heh).

As I say it is a minor one, and you can always take the view that a script with "Explicit" on cannot by definition have an optional parameter.

Top
#79488 - 2003-02-06 07:12 PM Re: Quoting Strings ??
Will Hetrick Offline
Hey THIS is FUN

Registered: 2001-10-02
Posts: 320
Loc: Harrisburg, PA USA
Glenn,
As I was looking at the statement you have used as a test,
? "X=$X, Y=" + $Y ?

I think that kix is seeing this statment as a string value + a string value since the $y is undeclared.
_________________________
You have at least 2 choices. Each choice changes your destiny. Choose wisely!

Top
#79489 - 2003-02-06 07:22 PM Re: Quoting Strings ??
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Glenn,
Not sure where Will was going with that...

Your examples don't align with what you're saying. The following code:
code:
$X = "first"
? "X=$X, Y=$Y" ?
get $_
$X = "first"
? "X=$X, Y=" + $Y ?
get $_

outputs the following:
quote:
X=first, Y=$Y

X=first, Y=

but you say "I would expect the interpreter to recognize $Y as an uninitialized variable and output Null, but it outputs "$Y" again".
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#79490 - 2003-02-07 03:41 AM Re: Quoting Strings ??
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
What the ? ? [Confused]

I was working with a rather complex script. It builds a command line to install a package of hotfixes as defined in an input file. The script reads the INI-format file and gets a list of hotfixes to apply. It then looks for each hotfix section to see if there are exceptions, different parameters, etc. before executing the hotfix file. It completes the process by running QCHAIN.exe. This has been working fine.

I decided to add some logging, since when we have our third-shift operations staff apply the hotfix package, the information scrolls by so fast they can't tell if it worked or not (they're "general" operators with Mainframe, Unix and NT skills - not NT admins.)

I'm not at work, so I'll pseudo-code - it works soft of like this:
code:
 
if $OPTION = "-l"
$LOG = " 2>&1>> LogFile.txt"
EndIf

some other code...

$HFIX = "123456i.exe" ; read fom file
$PARM = "-z"
$CMD = "%COMSPEC% /c $HFIX $PARM" + $LOG
? "Executing $CMD" ?
shell $CMD

At this time, when the shell command executes, it fails (unless I've passed the -l and defined the $LOG var) because the $CMD ends in "$LOG"

I fixed it by declaring $LOG="" at the beginning of the script, but I started doing the simple tests I illustrated before I posted the message. Now, at home (v4.12), it works the way you say (and the way I wanted it to!).

I'll review it again when I return to the office on Monday, but for now, I'm speechless.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#79491 - 2003-02-07 04:07 AM Re: Quoting Strings ??
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Take a look at REDIRECTOUTPUT() or the following UDFs: DisplayText(), WriteLog(), WriteLog2()
_________________________
There are two types of vessels, submarines and targets.

Top
#79492 - 2003-02-14 04:35 PM Re: Quoting Strings ??
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
Let me clarify, now that I'm back at work and can see the "problem".

Here's the code sample that I tested with:
code:
 
; undefined (pass 1)
$OTHER="" ; init NULL (pass 2)
$OTHER="other" ; defined (pass 3)

@KIX

? "This and than and $OTHER stuff"
? "This and than and " + $OTHER + " stuff" ?

The first time I ran this script, there were NO "$OTHER=" definitions, so $OTHER was undefined.

The second time, I added the definition to Null, whild the third time I changed the definition to contain text.

Here's the output from my 2K workstation:
code:
 
P1GAB01 - D:\Development>x
4.20 Release Candidate 3
This and than and $OTHER stuff
This and than and stuff

P1GAB01 - D:\Development>x
4.20 Release Candidate 3
This and than and stuff
This and than and stuff

P1GAB01 - D:\Development>x
4.20 Release Candidate 3
This and than and other stuff
This and than and other stuff

Again, what my original reference was is that an undefined string value is treated differently inside and outside of quotes. I would EXPECT that an undeclared string inside of quotes return NULL, as it does outside of quotes (and in other scripting environments). Basically - unless the engine sees "$$varname", it would output the value of "$varname" and not treat it as a literal.

Anyway, it was posted here to see if we could get the behaviour changed in a future release.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#79493 - 2003-02-14 04:50 PM Re: Quoting Strings ??
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Well shame on you for using vars in strings. [Big Grin]

Personally, I think the SetOption('NoVarsInStrings','on') should be on by default so that the old legacy stuff can finally get behind us. If people want to use vars in strings, they should set it explicitly. That is the only way we can get people to change.

Ruud,
Please make it so. We will man the forum for the flood of 'KixTarts' [Wink] it may bite.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#79494 - 2003-02-14 05:23 PM Re: Quoting Strings ??
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
I'd even set both 'NoVarsInStrings' and 'Explicit' to 'On' as default.
_________________________
There are two types of vessels, submarines and targets.

Top
#79495 - 2003-02-14 06:56 PM Re: Quoting Strings ??
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
Well, Vars in Strings - I have a problem with vars out of strings without an explicit print statement..
To me
code:
 
$MSG

is not as intuitive as
code:
 
SomePrintCommand $MSG

when you want to print the $MSG string by itself.

Just my opinion, and probably only $0.02 worth today.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#79496 - 2003-02-15 04:14 PM Re: Quoting Strings ??
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
That's the price you pay for a free-format language [Wink]

Seriously, this is why I have a habit of coding writes to the console like this:
code:
? 'some message'
? $string

The '?' (new line command) reminds me that I intentionally print to the console. If something prints to the console and it doesn't have the '?' then I know that it's not supposed to do this.
_________________________
There are two types of vessels, submarines and targets.

Top
#79497 - 2003-02-18 12:08 AM Re: Quoting Strings ??
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
to richard...

yes, you can use explicit.
just do:
redim preserve $myCMDlineVar
_________________________
!

download KiXnet

Top
#79498 - 2003-02-18 09:33 AM Re: Quoting Strings ??
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
quote:
yes, you can use explicit.
just do:
redim preserve $myCMDlineVar

Ooerr. Works like a charm.

Top
Page 1 of 1 1


Moderator:  ShaneEP, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1046 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.07 seconds in which 0.026 seconds were spent on a total of 12 queries. Zlib compression enabled.