This idea will certainly work! I personally try to avoid using undef as an intentional value because: a) this adds a complication about exists(), defined() later in the code, b)If you have to export something with an "undef" value to something else other than Perl what do you use? or for that matter even it its Perl, why not 0 or 1 which is easier to parse? undef throws an extra third case into mix. Sometimes this makes sense like SQL's NULL, but why throw that in there if you don't need to? c) it doesn't save any memory or execution time.
We are getting a bit off topic, but this is a hash slice. To get this work with the =1 idea, I think something like the below is required. There isn't foreach, but there is still a loop (except in the enumerated first case).
I personally like the $hr->{$_} = 1; syntax because it makes clear that we are de-referencing a hash reference.
A short thing about hash slice is at
hash slice. My foreach loop can be replaced with this..
#foreach my $num (@$arr)
#{
#any one of these does the same thing...
#pick one...
@$hr{@$arr} = (1,1,1);
@$hr{@$arr} = map{1}@$arr;
@{$hr}{@$arr} = map{1}@$arr;
#}
Oh, the map{1} generates an "@something" that has the same number of "1's" as there are elements in the input @$arr although here it is "anon", having no explicit name. Here that output of the map is used (ie, it is not a "bare" map). In this case, there is still a loop and we've just made the situation more confusing. Clarity of code is important and just because some implementation uses fewer lines doesn't mean that it is faster. |