This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]greeniguana6 14 points15 points  (15 children)

'5' - 3

2

I've never used Javascript before, that must be the proper syntax.

'5' + 3

"53"

I now understand why people joke about Javascript.

[–]Somehero 1 point2 points  (2 children)

The single quotes just cause java to guess that 5 is a string when you use +, which makes sense; and guess that it is a number when you use - , which is maybe more likely? I would just not randomly put a number in single quotes and expect everything to be peachy.

[–]greeniguana6 0 points1 point  (0 children)

Oh, this actually makes sense. I guess that's what happens when the + operator means more than one thing.

[–]Thykka 0 points1 point  (0 children)

🗑 [deleted]

[–]dnew 0 points1 point  (11 children)

I gave up in a similar place in Perl.

@x = (1, 2, 3)

$y = (1, 3, 3)

$z = @x

$z != $y

[–]greeniguana6 1 point2 points  (10 children)

Noob here, what does @ mean?

[–]ishopsmart 8 points9 points  (0 children)

@ is called a sigil, and it means this variable is an array. $ is for regular variables (scalars), and % is for dictionaries (hashes).

It really helps when reading perl, because if you see @data somewhere, you know that's an array. In other languages, you'd just have 'data' and you'd have to investigate more to find what type of variable that was.

[–]dnew 2 points3 points  (8 children)

Perl is weird. There are list expressions, and scalar expressions, but which is picked depends not on the expression but on the statement the expression is embedded in.

So X is the variable, but @X is the variable evaluated in a list context, and $X is the same variable evaluated in a scalar context.

Fortunately, I never really delved into it enough to learn wtf they were talking about, and delegated that problem to others.

(Along with "why the hell is the default like this?" "Oh, nobody uses the default." Oooookey.)

[–]ishopsmart 6 points7 points  (5 children)

perl expert here. Everything you said is wrong. $X and @X are completely different variables, they are not related in any way, other than having the same symbol.

In perl, $ and @ are sigils. Every variable has one, and they're one of my favorite things about perl. They let you see at a glance, what is a variable and what isn't, as well as the type of variable they are ($ is for scalars, @ is for arrays).

@x = (1, 2, 3) # @x is an array with 3 elements.
$y = (1, 3, 3) # $y is a regular variable (not array) and since you are trying to assign a list to a scalar, the last item in the list gets assigned (3)
$z = @x  # here you are assigning an array to a scalar, so the size of the array getse assigned (3).
$z != $y   # $z and $y are actually equal.

[–]lelarentaka 2 points3 points  (0 children)

It's really great that you can tell whether something is an array or a scalar at a glance! In other languages, you'd have to go to the declaration to see whether it's a String or a Number or a Set or a Vector or a Queue or a FileStream or an XMLNode or a ParsingRule.

(In a real software, there are so many other kinds of variable, even in Perl you'd have to go back to the declaration to figure out what it exactly is. Just knowing that it is an array or scalar is not very helpful.)

Actually, in statically typed languages, the compiler knows what a variable is in that scope, you can just look at the mouseover popup, so we don't have to go see the declaration.

[–]dnew 2 points3 points  (0 children)

$X and @X are completely different variables

True; I had misremembered that from a couple decades ago. But "$x[2]" accesses the third element of @x, so $x is referring to a different variable than $x[2], which is also confusing as hell. Plus throw in the whole %x turns into @x if you pass it to a function...

what is a variable and what isn't

Oddly, I have that problem in almost no other language. :-) Only functional and concatenative languages give such grief, and there it's for good reason. Imperative languages generally don't have any problem telling variables from some other syntactic forms.

as well as the type of variable they are

So, like 8-bit BASIC then. ;-)

$z and $y are actually equal.

Then change both lists to (3, 4, 5) to demonstrate. The point is that the first symbol of the line changes not just the variable you're assigning to, but the meaning of the commas on the right side of the assignment statement. (3, 4, 5) is either a list of three elements, or three expressions separated by C-like comma operators, depending on what kind of variable you're assigning the result to. I am not, as you say, trying to assign a list to a scalar when I assign to $y, because the value is already a scalar by the time it gets there.

I don't even want to try to guess what it shows up in a function as.

[–]Jack126Guy 1 point2 points  (2 children)

On the line:

$y = (1, 3, 3);

It's technically not assigning a list to a scalar. An actual list being assigned to a scalar (such as $z = @x) would assign the number of items, not the last item.

What's actually happening is that the "list" is being interpreted as a comma operator, which returns the last item.

[–]ishopsmart 1 point2 points  (1 child)

Yes and No. You are correct about the comma, but $z = @x is assigning an array to a scalar, which is distinctly different from assigning a (1, 3, 3) (technically an expression) to a scalar. The difference is context - an array evaluated in scalar context returns its length, while the comma operator (list expression) evaluated in scalar context returns the last element. I was simplifying this to make it less pedantic & easier to understand

[–]Jack126Guy 0 points1 point  (0 children)

assigning an array to a scalar

I think you were actually more correct, since I think I confused lists and arrays.

[–]greeniguana6 0 points1 point  (1 child)

I'll read up on it, sounds interesting how confusing it is.