Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: for loop 'bad math expression'





On 2024-02-03 18:05, Lawrence Velázquez wrote:
	% var=abc

	% unset abc
	% print -- $((var))
	0

	% abc=1
	% print -- $((var))
	1

	% abc=1+1
	% typeset -p abc
	typeset abc=1+1
	% print -- $((var))
	2

	% abc='1.0 * PPID / SECONDS - RANDOM'
	% typeset -p abc
	typeset abc='1.0 * PPID / SECONDS - RANDOM'
	% print -- $((var))
	-10080.313725490196


That astonishes me.  I've never heard of any such thing.  Nobody tells you these things.

% var=abc; print -- $var
abc

% abc=1; print -- $((var))
1

# abc was a string, now it's the name of an integer:
% let abc+=3; print -- $((var))
3

# var still wants to be scalar:
% var+=3; print -- $var
abc3

# So once var has been touched directly the link to abc is broken:
% abc+=2; print -- $((var))
0

% abc=2; print -- $((var))
0

... I suppose there's a good reason for it, but that leaves me dumbfounded.

% Sonnet_1='From_fairest_flowers_we_desire_increase'; print -- $Sonnet_1
From_fairest_flowers_we_desire_increase

% From_fairest_flowers_we_desire_increase=1        

% print -- $((Sonnet_1))
1

... ok ...

Anyway, the lesson is just not to assign a glob expansion to an integer.  Never, ever.  Sorry for the trouble gentlemen.









Messages sorted by: Reverse Date, Date, Thread, Author