Search code examples
echojscriptwsh

WScript.Echo() only prints part of the string


I've been coding in the WSH for a couple days now and just recently the WScript.Echo() method started to only print parts of the string or it would put the end of the string at the beginning so that it's jumbled. I'm using Win7 pro x64. This is the function that contains the broken Echo() method:

function age() {
    if ( !/\d*/.test(specimen[AGE]) || !/\d*/.test(comp[AGE]) ) {
        WScript.Echo('ERROR: age value for specimen or comp not valid')
        WScript.Sleep(10000);
        WScript.Quit();
    }

    WScript.Echo("specimen[AGE] = ", specimen[AGE], " and comp[AGE] = ", comp[AGE])

    ageAdjValue = ( parseInt(specimen[AGE]) * 250) - ( parseInt(comp[AGE]) * 250)
    adjArray[AGE] = ageAdjValue;

    //log adjArray
    for (i=0; i < adjArray.length; i++) {
        WScript.Echo(adjArray[i])
    }
}

This is the output:

  and comp[AGE] =  12
SKIP
SKIP
SKIP
SKIP
SKIP
0
0
?
0
0
0
2500
0
0

The message at the top doesn't include the specimen[AGE] part, instead it has blank space.

Now to see if it was because of what was contained in the specimen[AGE] element, I changed it to specimen[ADDRESS] like this:

WScript.Echo("specimen[AGE] = ", specimen[ADDRESS], " and comp[AGE] = ", comp[AGE])

This was the new output:

  and comp[AGE] =  12ings Ct.
SKIP
SKIP
SKIP
SKIP
SKIP
0
0
?
0
0
0
2500
0
0

It put the address at the end instead of the beginning and only printed part of the address...

I then noticed that in the string preceding specimen[ADDRESS], the string didn't match up with the variable... and for the hell of it I changed it to "specimen[ADDRESS]" to match, like this:

WScript.Echo("specimen[ADDRESS] = ", specimen[ADDRESS], " and comp[AGE] = ", comp[AGE])

Now I am completely clueless why... but the output changed to include 2 extra characters of the full address at the end...

  and comp[AGE] =  1263 Kings Ct.
SKIP
SKIP
SKIP
SKIP
SKIP
0
0
?
0
0
0
2500
0
0

The same thing happens when I use the + operator to concatenate the strings instead of commas. What could be causing this?


Solution

  • (1) The effect of seemingly missing parts of WScript.Echo'ed strings is caused by embedded "\r" in the data:

    WScript.Echo("A", "BB", "CCC");
    WScript.Echo("A\r", "BB", "CCC");
    =>
    A BB CCC
     BB CCC
    

    (2) To check for suspects, 'quote' the data:

    WScript.Echo(">" + "A" + "<");
    WScript.Echo(">" + "A\r" + "<");
    =>
    >A<
    <A
    

    (3) It's never 'the same' when you WScript.Echo a list of parameters (space separator) vs a concatenation (no space separator):

    WScript.Echo("A", "BB", "CCC");
    WScript.Echo("A" + "BB" + "CCC");
    =>
    A BB CCC
    ABBCCC
    

    (4) /whatever*/ - i.e. looking for zero or more whatevers in a string via a RegExp - is useless most of the times:

    WScript.Echo(/\d*/.test("full of sequences of zero or more digits"));
    WScript.Echo(/\d+/.test("full of sequences of zero or more digits"));
    WScript.Echo(/\d+/.test("full of 1 sequences of one or more digits"));
    =>
    -1
    0
    -1