3 ms·
Let me get at this Socratically. What's the syntax to pass a hash to a function, and use it there? Syntax for putting a hash inside an array, or vice-versa? How
by AceJohnny2 4y ago
Let me get at this Socratically. What's the syntax to pass a hash to a function, and use it there? Syntax for putting a hash inside an array, or vice-versa? How do you get the keys of a hash that's inside an array?
- wruza 4y agoWhat's the syntax to pass a hash to a function, and use it there? func %h; sub func { my %h = @_; Syntax for putting a hash inside an array, or vice-versa? None that I know of. But you can put a hashref into a list and vice versa: push @a, \%h; # or {…}, $hr $h{key} = \@a; # or […], $ar How do you get the keys of a hash that's inside an array? keys %{$a[0]} Your question could also mean keys %{{@a}} though less likely. But my perl fu weakened after years of wandering and I also remember reading that refs became first-class for common operators and functions (including keys), so dereferencing is not mandatory anymore. Not sure if that’s true, could be a dream easily.
- AceJohnny2 4y agofunc %h; sub func { my %h = @_; Note that the above only works if %h is the only parameter to the function, because your solution swallows the entire parameter array. Otherwise you must pass a hashref. In any case, these are examples of my discontent with Perl's syntax, which gets unreasonably complicated for non-trivial datastructures. To be clear, Perl's references, the \@a or \%h with the backslashes, are what I derisively call "pointers", because they introduce the same kind of complexity. It's a problem when you must use different syntaxes to update/refer to a hash, depending on the scope you're using it: $hash{$key} vs $hashref->{$key} The first syntax is if you have direct access to the %hash, such as the scope that created it. The second syntax is whenever you're forced to pass the %hash as a ref, for example because it's one of many parameters to a function. (And the hashref syntax I used is sugar! The basic syntax, demonstrated with the "hash-in-array" example, is worse) It's a problem when you have to "cast" a type, like in `keys %{$a[0]}` (for the audience: where $a[0] refers to a hashref inside an array, which has to be dereferenced via the %{} syntax, to be used as a hash). This is a simple example, but replace $a[0] with a more sophisticated datastructure, and things get exponentially unwieldy. The above make it harder to keep track of data as you're browsing code. In contrast, Python's everything-is-call-by-reference approach means that, whatever the scope, you can refer to something the same way. There never is differing syntax between 'something' and 'a reference to something'.
- AceJohnny2 4y ago> I also remember reading that refs became first-class for common operators and functions (including keys), so dereferencing is not mandatory anymore. This is intriguing, but I can't find any indication of this in Perl's own canonical `perlref` doc, or elsewhere