0
$\begingroup$

This is my code

xyzzy = 1;
Hold[xyzzy] // ToString // StringLength

The output is 11.

This 2nd one below.

ClearAll[xyzzy]
 ToString[xyzzy] // StringLength]

The output is 5.

Why is this?

I think these 2 codes will be the same output

$\endgroup$
11
  • 1
    $\begingroup$ the result of Hold[xyzzy] is Hold[xyzzy] and that is 11 characters long. When xyzzy has not been assigned any value then the ToString[xyzzy] will be the string xyzzy and that is 5 characters long. $\endgroup$ – Bill Sep 4 '20 at 2:24
  • 1
    $\begingroup$ @Bill How can I fix this? How can make it have the same output? $\endgroup$ – kile Sep 4 '20 at 2:28
  • 1
    $\begingroup$ What output is the desired? 5, 11, or 1? $\endgroup$ – xzczd Sep 4 '20 at 3:03
  • 1
    $\begingroup$ Possibly HoldForm[xyzzy] // ToString // StringLength does what you want? $\endgroup$ – Carl Woll Sep 4 '20 at 4:16
  • $\begingroup$ Hold[xyzzy] // ToString // StringLength[#] - 6 &? $\endgroup$ – kglr Sep 4 '20 at 5:54
2
$\begingroup$

Your first code acts as below line:

StringLength["Hold[xyzzy]"]

And second one acts as below line:

StringLength["xyzzy"]

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.