[net.unix-wizards] More C-shell weirdness Another word count problem

nose@nbires.UUCP (Steve Dunn) (04/01/86)

And now for another issue of "Can you explain this" from
whinin' Steve Dunn.

This:
set a = ''
set a = ($a '')
echo $#a

Yields 1

But this:

set a = ('' '')
echo $#a

Yields 2

This curious behavior has not actually caused me any trouble but I
am a bit puzzled


	         -Steve "Grep-face" Dunn

jsdy@hadron.UUCP (04/03/86)

In article <684@nbires.UUCP> nose@nbires.UUCP (Steve Dunn) writes:
>set a = ''
>set a = ($a '')
>echo $#a
>
>Yields 1
>
>set a = ('' '')
>echo $#a
>
>Yields 2

set a = ''
set a = ("$a" '')
echo $#a

2

Note that $a, where a is empty or containing all manner of
white space, gets fed back into the parser just as it appears:
as no characters or white space.  To be taken seriously, it
must appear in double quotes (which does sometimes cause
problems).

Consider:
	if ($a == "") ...
	if ("" == "$a") ...
I use the latter form, partly because $a might be -x or something
(!).  The former form could all too easily turn into
	if ( == "")
if what you  r e a l l y  wanted to test was true.  Testing this,
I am enthralled to find that if ( == "") and even if ( == ) both
test out true in csh!!!  I guess this is because of the parsing
that occurs at different times, as I mentioned in another letter.
This gives a syntax error in any sh that I've ever used: Thompson,
Mashey, Bourne, or Korn.
-- 

	Joe Yao		hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP}

jwp@sdchem.UUCP (John Pierce) (04/03/86)

In article <684@nbires.UUCP> nose@nbires.UUCP (Steve Dunn) writes:

	> set a = ''
	> set a = ($a '')	 set a = ('' '')
	> echo $#a		 echo $#a
	> 
	> Yields 1		 Yields 2

and asks for an explanation....  In the first case $a is expanded to a null
string before the assignment is done, and, therefore, isn't seen as a string
at the time the assignment is made; there is no such thing as an unquoted
null string (this is implied, though not stated explicitly in exactly those
words by csh(1), page 1, "Lexical Structure").  Note that

	set a = ''
	set a = ( "$a" '' )
	echo $#a

yields 2 since the null string that is $a is quoted and is, therefore, a word.

				John Pierce, Chemistry, UC San Diego
				jwp%chem@ucsd.edu
				sdcsvax!sdchem!jwp

karl@cbrma.UUCP (Karl Kleinpaste) (04/03/86)

In article <684@nbires.UUCP> nose@nbires.UUCP (Steve Dunn) writes:
>This:
>set a = ''
>set a = ($a '')
>echo $#a
>Yields 1
>
>But this:
>
>set a = ('' '')
>echo $#a
>Yields 2
>
>This curious behavior has not actually caused me any trouble but I
>am a bit puzzled

It's because of the order in which things are being substituted in the
input line.  That is, what is happening is that
	set a = ($a '')
becomes
	set a = ( '')
when $a's empty value is stuffed into the line.  Then the
interpretation of the remainder gives `a' the single value ''.

As a counterpoint, try
	set a = ''
	set a = ("$a" '')
	echo $#a
which yields 2.  The reason is that you've stipulated extra quotings,
which preserve values for longer than the simple replacement of $a in
the input line with its null value.  The use of double quotes causes
$a to be interpreted within them, subsituting its null string, while
leaving that null string quoted, so that two null strings result.
-- 
Karl Kleinpaste

chris@umcp-cs.UUCP (Chris Torek) (04/04/86)

In article <684@nbires.UUCP> nose@nbires.UUCP writes:
>set a = ''
>set a = ($a '')
>echo $#a
>
>Yields 1
>
>set a = ('' '')
>echo $#a
>
>Yields 2

It is all a matter of quoting:

	set a = ''; set a = ("$a" ''); echo $#a

gives `2'.  Your first example gives `1' because the C shell
does history substitution (none), then variable expansion---now
the command is

	set a = ( '')

---then alias expansion (none), and finally executes it.  With
the double quotes, variable expansion yeilds

	set a = ("" '')

which is of course two words.

It is often important to quote variables being expanded.  For
instance:

	set a = '*'
	set a = ($a)
	echo $a

prints the same thing as `echo *'.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1415)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@mimsy.umd.edu