zsh-users
 help / color / mirror / code / Atom feed
From: "Lawrence Velázquez" <larryv@zsh.org>
To: zsh-users@zsh.org
Subject: Re: for loop 'bad math expression'
Date: Sun, 04 Feb 2024 15:48:42 -0500	[thread overview]
Message-ID: <1745fff9-822c-499f-b60e-7248e95438c5@app.fastmail.com> (raw)
In-Reply-To: <ec856c16-59c3-4967-b309-cb0da750354b@eastlink.ca>

On Sun, Feb 4, 2024, at 10:51 AM, Ray Andrews wrote:
> That astonishes me.  I've never heard of any such thing.  Nobody
> tells you these things.

I'm not sure if the zsh manual spells this behavior out explicitly,
but bash and ksh share it.


> # abc was a string, now it's the name of an integer:

These are not mutually exclusive.  The value of var remains "abc"
and does not change at any point.  The important thing is that it
is *interpreted* a certain way *in arithmetic contexts*.


> % let abc+=3; print -- $((var))
> 3

This result does not correlate with the commands you've actually
shown here.

	% var=abc
	% abc=1
	% let abc+=3
	% print -- $((var))
	4


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

It doesn't "want" to be scalar, it IS scalar.  But that's irrelevant.
What's relevant is that it does not have the integer attribute, so
"var+=3" performs string concatenation and not arithmetic incrementing.
You'd see the same thing with "abc", even though it looks like a
number.

	% abc=1
	% abc+=3
	% typeset -p abc
	typeset abc=13

(I don't know why all of a sudden you've switched from using "let"
to not using it.)


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

It's not about "touching", and there is no "link".  You CHANGED its
value to "abc3", and there is no such variable, so its arithmetic
value is recursively taken to be zero.


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

If you were given these algebraic equations and asked what the value
of "c" is, presumably you'd say "one" and not "the letter 'b'".

	a = 1
	b = a
	c = b

Same thing in shell arithmetic.

	% a=1
	% b=a
	% c=b
	% print -- $((c))
	1


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

There is no arithmetic context here, so you just get the raw value.


> % From_fairest_flowers_we_desire_increase=1        
>
> % print -- $((Sonnet_1))
> 1
>
> ... ok ...

There *is* an arithmetic context here, so it's interpreted as an
arithmetic expression.  What is confusing about this?  You're
intentionally using unwieldy names, but the principle is the same
as if you'd just used "a" and "b".


> Anyway, the lesson is just not to assign a glob expansion to an
> integer.

The real lesson might be that you should avoid the integer attribute
until you understand shell arithmetic better.


-- 
vq


  reply	other threads:[~2024-02-04 20:49 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-30  3:39 Ray Andrews
2024-01-30  3:46 ` Ray Andrews
2024-01-30  4:04   ` Bart Schaefer
2024-01-30  4:06     ` Bart Schaefer
2024-01-30  4:17       ` Ray Andrews
2024-01-30 13:44         ` Ray Andrews
2024-01-30 14:30           ` Lawrence Velázquez
2024-02-03 23:52           ` Bart Schaefer
2024-02-04  1:14             ` Ray Andrews
2024-02-04  2:05               ` Lawrence Velázquez
2024-02-04  4:20                 ` Bart Schaefer
2024-02-04 16:08                   ` Ray Andrews
2024-02-04 20:56                     ` Lawrence Velázquez
2024-02-04 15:51                 ` Ray Andrews
2024-02-04 20:48                   ` Lawrence Velázquez [this message]
2024-02-04 21:09                     ` Bart Schaefer
2024-02-04 21:23                       ` Bart Schaefer
2024-02-05  2:10                     ` Bart Schaefer
2024-02-05  2:43                       ` Mikael Magnusson
2024-02-05  2:50                         ` Bart Schaefer
2024-02-05 15:21                       ` Ray Andrews
2024-02-05 15:48                         ` Mark J. Reed
2024-02-04 14:43               ` Mark J. Reed
2024-02-04 16:37                 ` Ray Andrews
2024-02-04 21:09                   ` Lawrence Velázquez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1745fff9-822c-499f-b60e-7248e95438c5@app.fastmail.com \
    --to=larryv@zsh.org \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).