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

Re: for loop 'bad math expression'



Gentlemen:

I'm a bit behind here. I hadn't expected this to thread to get so involved.  We may have a bit of a mishmash here, thanks for your patience as I try to sort it out:

> Did you see my followup? The variable does not get "promoted"; it's still a string,  as you can tell by the fact that += appends to it instead of adding.

Right, 'var' isn't fiddled with.  It just seem that taking the value of var and using it as the name of a brand new variable seems a bit strange to me.  Who cares?  Even I don't care, all that matters is that I'm aware of the situation.  I don't like German grammar either but the Germans don't care.  At this point all that remains 'open' is that I think 'abc' should 'typedef -p' as an integer.  And it seems, as I said, that any direct assignment to 'var' breaks the relationship with 'abc'.

> When I wrote "x+=d", it was not inside arithmetic context, so the shell did not interpret either x or d as a number; it just appended the literal value "d" onto the end of the string held in x.

Right.  My own post included a demonstration of that.  I get it. Dunno, maybe the shell's internal machinery requires the creation of 'abc' as a new variable in order to perform the arithmetic.  ... Come to think of it, that makes sense.  I'd just not expect the new variable to be visible but that's a trivial matter.


    % var="abc+def"
    % abc=2
    % def=3
    % echo $(( var ))
    5

... that stretches my mind yet again.  Interesting that 'var' is completely passive as to what "abc+def" might possibly mean until such times as an answer is demanded by the echo.

    % var="abc+def"; echo $var
    abc+def

... what must be a simple string when written becomes an addition of two variables retroactively.  Ok ... if that's the way it is then that's the way it is.  Yeah ... within the $(()) the string 'abc+def' NOW becomes an addition of two variables.  Sure ... interpreted ... not C ... not retroactive either. more like indeterminate.

> and this interpretation came about quite naturally from some incremental improvements in the shell's ability to do arithmetic.

Right, as I said, arithmetic would have accreted onto the shell at some point, so had to be merged in with existing data types.  Sorta like an associative array is 'really' just a normal array under the hood, but given a different interpretation. Thus the way an A array is easy to break if you aren't careful.

> See, the shell has never really had data types like other programming languages.

Exactly.  But it takes deep surgery to cut the C-think out of the brain.  "abc+def" looks like a string is a string is a string.

> ... BASIC got the name from the way you introduce names for things in a math proof or paper.

The depth of your historical knowledge astonishes me Mark.

>Ok, not quite shut up yet.  One note:

    % z=`expr $x + $y`

... actually it's these tortures that are exactly the sort of therapy I need.  Too bad there wasn't some document: "Shell programing for folks who are used to strongly typed languages."

> No.  It "increments like one" because you ran "abc=1; let abc+=3",
and the arguments to "let" are interpreted as arithmetic expressions.
If you'd run "abc=1; abc+=3" you would have gotten string concatenation.

RIGHT!  Got it.

> Using
>  % let var=value
> always applies math context, just as ((...)) and  $((...)) and $[...] do.

... it's another gotcha ... I know and expect that $((()) has its internal grammar.  But 'let' seems ... naked ... for lack of a better word and it's a strain on grammatical rigor that it gets special, invisible favors.  Don't like it.  But it's one of those ancient traditions that's written in stone, I take it.

> All your examples have been broken, and your conjectures confused.

Yes.  One tries to form an inner model of what's going on.  Seems there's no Phlogiston after all ;-)

> ... This can have unforeseen consequences.

HA! Yes, that's a little bit of comfort for poor wretches like myself.  Like trying to get somewhere in Mexico City traffic, one simply needs to be very careful.  I expect if we had history to do over again, shells would have proper typing and all of this would be 10X simpler.

Thanks guys.





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