There's more than one way to do things | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
in Perl, you use () to construct an array () does not construct an array. In an array assignment, my @array = (1, 2, 3);, the right hand side is a list, not an array. The parentheses in this case are needed only because if you wrote my @array = 1, 2, 3;, precedence would cause that to be interpreted as (my @array = 1), 2, 3;, thus not storing the 2 and 3 in @array. Contrast this to [] that always create a new array and return a reference to it. The thing inside [] is also a list, not an array. Only here you don't need the parens, because there is no difference in precedence with or without. But if you want, you can of course write [ (1, 2, 3) ]. Also note how my @array = (1, 2, (3, 4)); and my $arrayref = [ 1, 2, [ 3, 4 ] ]; are different in that in the former, the four elements are flattened and in the latter, you have a nested data structure. The difference between the constructors [] and {} on one hand and the grouping () on the other, is reason enough for me to write them differently. That is why I put extra whitespace inside the constructors. In the third case, you end up with a variable that holds the length of the array on the RHS. Firstly, the RHS is not an array. In scalar context, it is not even a list, because a list can only exist in list context. Because it's not an array and not a list, it is impossible to get the number of its elements. Instead, you get the last value supplied. Your mistake here was to use 1, 2, 3. If you then get 3, you can't immediately know how to interpret it. In this kind of example or experimental code, always use values that do not naturally occur. Do not begin with 1 and do not end with the number of items you provide. Along those lines you should also not use something like 0, 1, 2. Instead, try a set like 42, 15, 69. why is length(@foo) perfectly acceptable syntax? Because there's nothing that says you shouldn't use an array in scalar context. In scalar context, an array evaluates to its number of elements. That may not be a useful value to provide to length, but it is to many other functions. And to get a consistent language, it shouldn't be made invalid to determine the number of digits of a number of elements. ince in Python you do len(lst) to calculate the length of the list variable lst Python does not have context, which is the fundamental difference. It cannot see the list variable as anything else than a list variable, and a function can not be smart about what it returns depending on the context in which it is used. Note, by the way, that Python's list is like Perl's array. In Perl, we have both arrays and lists and they are not the same thing! Why can't Perl's "length" function be reasonable? It is perfectly reasonable, it just doesn't do what you expected. And that is because your expectations are based on a Python world of physical laws, while Perl functions do not live in that world. They live in a Perl world, where Perl's laws and principles are used. Of course, to a traveler, a new world and its culture may at first seem very weird. But as a traveler, you should know better than to judge so quickly! Learn our culture, including context and the difference between arrays and lists, and eventually you will feel right at home. Here is an incomplete list of things, copied from http://juerd.nl/perladvice, that you will need to understand:
Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' } In reply to Re: Some Insights from a Traveler Between Languages
by Juerd
|
|