Re: The philosophy behind element reference syntax
by robharper (Pilgrim) on Dec 08, 2004 at 16:14 UTC
|
I am also new to Perl, but the way I see it is that the prefix tells me whether the result I am going to be playing with is a scalar or a list. Thus...
Something starting with $ is a scalar -- the parentheses or whatever after the identifier tell me that it comes from an array (or hash or whatever), but the value returned is a scalar nonetheless.
Similarly, if there is an @ preceding an identifier, the value I am playing with is a list -- it may be from an array, an array slice, or whathaveyou, but it is still going to be a list.
I'm sure I am oversimplifying, but it's a way of looking at things that works for me in the simple programs I have written so far.
| [reply] |
|
|
| [reply] |
Re: The philosophy behind element reference syntax
by tall_man (Parson) on Dec 08, 2004 at 16:00 UTC
|
You're in good company on that suggestion. Larry Wall thinks so, too. It will be in Perl 6, as described for example here. | [reply] |
|
|
| [reply] |
Re: The philosophy behind element reference syntax
by amt (Monk) on Dec 08, 2004 at 15:59 UTC
|
One way to look at the array element is to consider other languages. If you want to access an array element, in most high level languages (C,Java) you would access it as foo[0]. Now, considering PERL's behavior, how do you want to access the memory block that contains foo[0], you want to access it as a scalar ($). This covers both simple arrays, as well as arrays that contain hashrefs and arrayrefs, as those are scalar references to memory locations.
That's how I got over that hump as a beginner, I hope that this could help.
| [reply] |
|
|
| [reply] [d/l] |
|
|
i don't get it, it looks like some sarcastic code reference regarding the proper spelling of Perl. ;)
| [reply] |
|
|
|
|
Re: The philosophy behind element reference syntax
by NetWallah (Canon) on Dec 08, 2004 at 17:04 UTC
|
First, you are in good company to be thinking along these lines - Most perl newbies get tripped on the syntax.
It helps if you understand how Perl stores the variables - i.e. the program's namespace:
The NAME is simply "days". When you want to access a variable named "days", you can think of the "$" as a de-reference operator, so asking for "$days" says - "go to the namespace, and fetch the scalar named "days". Similarly, asking for "@days" requests and array named "days" - with the understanding that there is a separate namespace for arrays and scalars, so two different data types can have the exact same name.
Before I get flamed - this is a simplified explanation, and there is a lot more to the namespace - but thinking of it in these terms made it simpler for me to resolve issues.
Also remember that perl selects namespace based on CONTEXT. So, if I want an element of an array, (days[6]), if you think that perl sees the subscript before looking at the $ or @ in front of it, it knows which namespace to fetch from. Hence, it is NOT confused by a request for $days[6] (although a human might be).
Update:Finally figured out how to display the [ and ] characters - use "[" and ] for ] Thanks to rinceWind who also pointed this out, and revdiablo who provided a way to show & to explain THIS note.
...each is assigned
his own private delusion but he cannot see the baggage on his own
back.
| [reply] |
Re: The philosophy behind element reference syntax
by gaal (Parson) on Dec 08, 2004 at 16:00 UTC
|
These syntaxes do in fact work. But taking a single element can be made faster than taking a slice, so if know you need single element subscripting, that style is preferred. This, at least, is true for perl5; in perl6 it will work the way you want. | [reply] |
Re: The philosophy behind element reference syntax
by Anonymous Monk on Dec 08, 2004 at 17:38 UTC
|
If you really want @array[$i], which is something that I will probably dislike in Perl 6 then you can use the module Perl6::Variables
What follows are some useless arguments why I think $array[$i] is good...
Because you are refering to a scalar. An array (and a hash) can only contains a scalar.
By refering to a single element with $ you have the advantage (or not?) that you know that the prefix of the right part of the assigment has to be the same as the prefix of the left part.
This last thingie might make it easier to spot bugs if you are storing an array slice in a scalar (or not?).
If you put an array slice on the right part of the assigment (after the = that is) then it will return the last value, which might not be what you expected at all...
This would also enable you to build your own code checker which would check if each part of the assigment uses the correct prefix.
| [reply] [d/l] [select] |
Symbols are about denoting context
by nevyn (Monk) on Dec 08, 2004 at 17:48 UTC
|
It's all changing the context you are in, you can reference an element as @foo[$i] or $foo[$i], it's just an array slice of one (Ie. array context) versus a single element (scalar context).
Eg.
my @abcd = (1,2,3);
print "@abcd" . "\n";
$abcd[0] = 4;
@abcd[1] = 5;
print "@abcd" . "\n";
$abcd[0] = (4, 5, 6);
@abcd[1] = (4, 5, 6);
print "@abcd" . "\n";
print @abcd . "\n";
__DATA__
1 2 3
4 5 3
6 4 3
3
As you can see the simple assignments are the same, it's only when you get to a point where the context matters that it changes.
| [reply] [d/l] |
|
|
Your post is mostly correct, except for a small nit that I can't help but pick -- it's called "list context," not "array context."
I know people hate hearing this, but there is a difference between lists and arrays, and it can be rather important. It doesn't help that the context determination is made by a function called wantarray, but I think we should try our best to use the correct terminology.
| [reply] |
Re: The philosophy behind element reference syntax
by ikegami (Patriarch) on Dec 08, 2004 at 17:08 UTC
|
I view it as follows: The symbol indicates the *returned* type, like CHR$ in the BASIC's past.
In any case, Perl 6 will use @ary[0] rather than $ary[0] for both single elements and slices. IIRC, Perl 6's $ary[0] will be the same as Perl 5's $ary->[0].
| [reply] [d/l] [select] |
|
|
| [reply] |
Re: The philosophy behind element reference syntax
by mpeters (Chaplain) on Dec 08, 2004 at 17:18 UTC
|
It should be 'the'/'this' vs 'these'/'those'
It's singular vs plural (that's what scalar vs vector means anyway). If you are thinking of it as 'this' then you are thinking of it right. You just need to realize that '$' eq 'this' in perl. | [reply] |
|
|
| [reply] |
|
|
Mind you, the word this already has plenty of meaning in many other languages; it just so happens that Perl OO typically uses $self in such situations.
-- [ e d @ h a l l e y . c c ]
| [reply] [d/l] [select] |
Re: The philosophy behind element reference syntax
by Tommy (Chaplain) on Dec 09, 2004 at 01:57 UTC
|
Perl 6 will do what you seem to desire (@ary[0]). Evidently lots of other folks do too.
--
Tommy Butler, a.k.a. TOMMY
| [reply] [d/l] |
Re: The philosophy behind element reference syntax
by robot_tourist (Hermit) on Dec 10, 2004 at 09:24 UTC
|
s/use strict;/#use strict;/
But seriously, to me the problem seems to be associating the sigil with the variable's name.
How you name your variables is also important, scalars should be singular and arrays should be plural as much as possible.
As an aside, when I first saw Perl and all it's sigils I thought it was quite an ugly language, but any code can look good if it's well formatted and I miss the sigils if I go back to Java. When I write in Object Pascal, I put a prefix onto variable names to indicate their type.
How can you feel when you're made of steel? I am made of steel. I am the Robot Tourist. Robot Tourist, by Ten Benson
| [reply] [d/l] |