### Re^2: Numification of strings

by LanX (Bishop)
 on Aug 02, 2010 at 11:28 UTC ( #852448=note: print w/replies, xml ) Need Help??

in reply to Re: Numification of strings

>This behavior seems consistent and indeed in/of use, for instance, consider when quickly wanting to start a counter:

Sure that's useful, but it's only about converting undef or (maybe sometimes) the empty string ''.

Cheers Rolf

Replies are listed 'Best First'.
Re^3: Numification of strings
by ww (Archbishop) on Aug 02, 2010 at 12:33 UTC
"but it's only about converting undef"

Lest you or anyone else read that as generally true, I repeat, for emphasis re warnings (or for brevity here, -w) and re 'consistent':

```perl -we "\$x="LanX"; print (\$x * 3);"
Argument "LanX" isn't numeric in multiplication (*) at -e line 1.
0
perl -we "\$x="LanX"; print (\$x % 3);"
Argument "LanX" isn't numeric in modulus (%) at -e line 1.
0
perl -we "\$x="LanX"; print (3^\$x);"
Argument "LanX" isn't numeric in bitwise xor (^) at -e line 1.
3
perl -we "\$x="LanX"; print (3**\$x);"
Argument "LanX" isn't numeric in exponentiation (**) at -e line 1.
1

but

```perl -we "\$x='7LanX'; print (3^\$x);"
Argument "7LanX" isn't numeric in bitwise xor (^) at -e line 1.
4
perl -we "\$x='7LanX'; print (\$x*3);"
Argument "7LanX" isn't numeric in multiplication (*) at -e line 1.
21
perl -we "\$x='7LanX'; print (3**\$x);"
Argument "7LanX" isn't numeric in exponentiation (**) at -e line 1.
2187

and where it's useful -- using a string as a number because it's being used in an operation which is arithmetic:

```perl -we "\$x=\"5\"; print (\$x*3);"  # 2
15
perl -we "\$x='5'; print (\$x*3);"
15

2   Interesting (or not enough coffee yet?): doze balked at doublequotes without escapes here, but passed 'em cheerfully above. WTF?

> > "but it's only about converting undef"

converting undef is differently handled with ++, enabling warnings doesn't help here!

Cheers Rolf

More like "doesn't hurt".
```++\$count{\$_} for @list;
would have to be written
```\$count{\$_} = \$count{\$_}//0 + 1 for @list;
Re^3: Numification of strings
by LanX (Bishop) on Aug 02, 2010 at 12:38 UTC
wow incrementing with ++ is not orthogonal to +1!

```  DB<33> my \$y;\$y++

DB<34> my \$y;\$y+1
Use of uninitialized value \$y in addition (+) at (eval 38)[/usr/share/
+perl/5.10/perl5db.pl:638] line 2.

from perlop

```        The auto-increment operator has a little extra builtin magic t
+o it.

...(<i>special magic for incrementing strings, ie <c>++(\$x='x9') eq 'y
+0'</i>)...

"undef" is always treated as numeric, and in particular is chan
+ged to 0
before incrementing (so that a post-increment of an undef value
+ will
return 0 rather than "undef").

Cheers Rolf

Orthogonal means unrelated, independent. For example, if the size of a fish is orthogonal to its coloring, knowing the colour of a fish sheds no information about its size and vice-versa.
> > wow incrementing with ++ is not orthogonal to +1!

My wording was bad, I meant that the break of symmetry of '++' and '+1' "lowers the orthogonality" of the language.

This comment from a Java board reflects my understanding of orthogonal:

Orthogonality is a boolean property. You have it or you don't. It is not a number or count. ...

What Wirth specifically meant was that Pascal control statements and declarations are independent of each other. This is the reason why Pascal compilers can be so small. See here.

The concept in programming languages comes from Van Wijngaarden's paper Orthogonal design and description of a formal language prepared for the Algol-68 group, in which he expressed the desire to 'see the definition of a (programming) language as the Cartesian product of its concepts'.

And if features of the concept of addition are not parallel, the product of the features becomes "less" Cartesian, i.e. one gets "more exceptions from the rule". That's my personal definition of "lowering orthogonality".

Cheers Rolf

UPDATE: to be more explicit:

```:~\$ perl -wde0
...
DB<1> my %a; \$a{1}=\$a{1}+1;print \$a{1}
Use of uninitialized value \$a{"1"} in addition (+) at (eval 5)[/usr/sh
+are/perl/5.10/perl5db.pl:638] line 2.
...
1
DB<2> my %a; \$a{1}++;print \$a{1}
1

in perl lingo you might say ++ has special magic!

Create A New User
Node Status?
node history
Node Type: note [id://852448]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2018-04-19 16:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (74 votes). Check out past polls.

Notices?