Assigning duplicate values to several hash keys using junctions?
Hello all, I was curious if this is possible in Perl 6: %hash{ 'foo' 'bar' } = 'some value'; # %hash{'foo'} eq 'some value' and %hash{'bar'} eq 'some value' or perhaps %hash{ 'foo' | 'bar' } = 'some value'; In other words, is it possible to assign the same value to multiple hash keys simultaneously using junctions (or some other new mechanism)? In Perl 5, I would do $hash{'foo'} = $hash{'bar'} = 'some value'; which gets tedious with more than one hash key. An alternative is always @hash{qw(foo bar)} = ('some value') x 2; which is probably %hashfoo bar = ('some value') x 2; in Perl 6, but you always need to take care to write the correct integer in the list replication. The best way is, of course, (in Perl 5) { my @keys = qw(foo bar); @ha...@keys} = ('some value') x @keys; } but then you need the @keys array, which needs to be defined if you are dealing with literal values. Reading the synopses, one possibility seems to be %hashfoo bar = 'some value'; using hyper operators, but is that really the best way? Regards, Ville Koskinen
Re: Assigning duplicate values to several hash keys using junctions?
2009/6/8 Ville Koskinen vrk...@iki.fi: Hello all, I was curious if this is possible in Perl 6: %hash{ 'foo' 'bar' } = 'some value'; # %hash{'foo'} eq 'some value' and %hash{'bar'} eq 'some value' By autothreading, this would be equivalent to: (%hash{'foo'} %hash{'bar'}) = 'some value'; which in turn is the same as: (%hash{'foo'} = 'some value') (%hash{'bar'} = 'some value'); So yes, this is possible. -- Jonathan Dataweaver Lang
Re: Assigning duplicate values to several hash keys using junctions?
On Mon, Jun 08, 2009 at 12:02:43PM +0100, Ville Koskinen wrote: : An alternative is always : : @hash{qw(foo bar)} = ('some value') x 2; : : which is probably : : %hashfoo bar = ('some value') x 2; : : in Perl 6, but you always need to take care to write the correct integer : in the list replication. Two problems. First, list replicaiton is xx rather than x, and second, you *don't* have to write the integer, because you can use the Whatever star: %hashfoo bar = 'some value' xx *; That makes an arbitrarily long list of strings. : The best way is, of course, (in Perl 5) : : { :my @keys = qw(foo bar); :@ha...@keys} = ('some value') x @keys; : } : : but then you need the @keys array, which needs to be defined if you are : dealing with literal values. Perl 6's new * term is useful in many such places where you just want to say, I dunno, you figure it out. : Reading the synopses, one possibility seems to be : : %hashfoo bar = 'some value'; : : using hyper operators, but is that really the best way? If you'll define best way, I'll tell you if it is. :) The relative efficiency is going to be difficult to predict because any of these could be poorly implemented and do too much busywork. Apart from that, it's gonna come down primarily to what you think is readable. By the way, infix hypers want to go on both sides, like this: %hashfoo bar »=» 'some value'; Larry