5 ms·
This makes me appreciate autovivification and casting in perl so that you can just say "$color_counts{$color} += 1" without all the initialization.
by buzzdenver 11y ago
This makes me appreciate autovivification and casting in perl so that you can just say "$color_counts{$color} += 1" without all the initialization.
- collyw 11y agoI miss that in Perl a lot.
- jboggan 11y agoYes! This was one of the biggest things I missed when I moved to Ruby. I try and tell people how great autovivification is but unless they've coded with it the feature just sounds strange. But it lets you build some really great data structures on the fly!
- kbenson 11y agoI love it as well, and would hate to do without it, but it does come with it's own warts. Such as this: use Data::Dumper; my %h; if ( $h{foo}{bar}{baz} ) { say "Never happens"; } say Dumper \%h; And you get this: $VAR1 = { 'foo' => { 'bar' => {} } };
- colonelguy 11y agoYou'd want to use the exists operator in that case. It checks if the hash key is present without auto vivifying it. my %hash = (); if (exists $hash{foo}) {print "This doesn't run!";}
- kbenson 11y agoI'm well aware of exists, and how it functions, and that it in no way solves the problem presented. Exists tests for the existence of a hash key, so you can tell if it exists but is possibly undefined, but it does not stop autovivification in any way. Your example doesn't cause autovivification even without exists. Autovivification is the automatic creation of the underlying hashes and arrays in a multiple level data structure when they are used while accessing a nested data-structure. For example, given an empty hash %hash, $hash{foo} does not cause autovivification, but $hash{foo}{bar} will automatically create an empty hash and assign a reference to it to $hash{foo}.
- raiph 11y agoFwiw Perl 6 only autovivifies anything, including intermediary data structure levels, when writing to a data structure. So nothing happens in this case: my %h; dd %h if %h<foo><bar><baz> { say "Never happens" } dd %h prints: Hash $var = {} Hash $var = {}
- kbenson 11y agoNice, I wasn't aware of that feature, but the features I'm still not aware of in Perl 6 could fill a small book, since I still haven't gotten around to using it for much. :/
- thomasahle 11y agoSo how do I know if "1" gets added to a 0 or to an empty string?
- mapleoin 11y agoI suppose there's some type inference there and you'll get the default be 0 since 1 is also an integer, not a string.
- colonelguy 11y agoPerl uses separate operators for mathematical addition and string concatenation. 1 + 1 == 2; 1 . 1 eq 11; It uses separate operators for most mathematical and string operations, for that matter. There isn't much of a meaningful difference between strings and numbers in Perl -- it converts between then depending on the operation.
- antod 11y agoAsking as a Perl ignoramus, what does this give you over the defaultdict(int) example from the article? Does autovivification mean that the default value to use depends on the operation? eg 0 for addition and "" for concatenation?
- buzzdenver 11y agoI'm a Python ignoramus, but I think they're the same, except for Perl's being more concise. How would you do a 3-level hash though in Python ? What you call "value to use depends on the operation" is called casting. Depending on the context, a variable can be different values. For example a variable that has been defined but nothing else is treated as 0 if you try to add a number to it. It's an empty string if you use it in a string operation. Wikipedia explains it well: autovivification is the automatic creation of new arrays and hashes as required every time an undefined value is dereferenced. Perl autovivification allows a programmer to refer to a structured variable, and arbitrary sub-elements of that structured variable, without expressly declaring the existence of the variable and its complete structure beforehand.
- antod 11y ago> I'm a Python ignoramus, but I think they're the same, except for Perl's being more concise. Isn't everything always more concise in Perl? No matter what language it's compared to :) > How would you do a 3-level hash though in Python ? You might/should be able to wrangle that with some customisation of the default_factory or a defaultdict subclass, but yeah the arbitrary depth wouldn't be as 'plug n play' as with Perl. > What you call "value to use depends on the operation" is called casting. Depending on the context, a variable can be different values. For example a variable that has been defined but nothing else is treated as 0 if you try to add a number to it. It's an empty string if you use it in a string operation. Yeah Python's strong typing (usually) balks at automatic casting. You'd need to choose what the default is.
- kbenson 11y ago> Isn't everything always more concise in Perl? No matter what language it's compared to :) Ha, you need more exposure to things like APL :)