"George" == George Bouras <foo@example.com> writes:
I want to iterate the unique values of a hash, without poluting the
code with extra hash definition. Any better idea than
my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );
foreach ( sub{local $_={}; @{$_}{@_}=1; keys %{$_}}->(values %hash) )
{ say $_; }
"George" == George Bouras <foo@example.com> writes:
George> I want to iterate the unique values of a hash, without poluting George> the code with extra hash definition. Any better idea than
George> my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );
George> foreach ( sub{local $_={}; @{$_}{@_}=1; keys %{$_}}->(values
George> %hash) ) { say $_; }
Yes. Anything *but* that. You're not golfing here.
my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );
{
my %u;
@u{values %hash} = ();
print "$_\n" for sort keys %u;
}
And even *that* could use a comment or two for junior devs.
print "Just another Perl hacker,"; # the original
El 23/2/21 a las 16:27, Randal L. Schwartz escribió:
"George" == George Bouras <foo@example.com> writes:
George> I want to iterate the unique values of a hash, without poluting
George> the code with extra hash definition. Any better idea than
George> my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );
George> foreach ( sub{local $_={}; @{$_}{@_}=1; keys %{$_}}->(values
George> %hash) ) { say $_; }
Yes. Anything *but* that. You're not golfing here.
my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );
{
my %u;
@u{values %hash} = ();
print "$_\n" for sort keys %u;
}
Doesn't this do the same?
my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );
my %v = reverse %hash;
for (keys %v){
print "$_\n";
}
gamo <gamo@telecable.es> writes:
El 23/2/21 a las 16:27, Randal L. Schwartz escribió:
"George" == George Bouras <foo@example.com> writes:
George> I want to iterate the unique values of a hash, without poluting
George> the code with extra hash definition. Any better idea than
George> my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );
George> foreach ( sub{local $_={}; @{$_}{@_}=1; keys %{$_}}->(values
George> %hash) ) { say $_; }
Yes. Anything *but* that. You're not golfing here.
my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );
{
my %u;
@u{values %hash} = ();
print "$_\n" for sort keys %u;
}
Doesn't this do the same?
my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );
my %v = reverse %hash;
for (keys %v){
print "$_\n";
}
It's mostly a more contorted, functional equivalent. The difference is
that the output order of the loop is not defined.
with <s1339k$rhi$1@gioia.aioe.org> George Bouras wrote:
I want to iterate the unique values of a hash, without poluting the
code with extra hash definition. Any better idea than
my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );
foreach ( sub{local $_={}; @{$_}{@_}=1; keys %{$_}}->(values %hash) )
{ say $_; }
By my books 'local $_={}' of yours is no different from 'my %foo'. IOW,
it's not fulfilling requirements "without ... definition" -- you're not
going 'local' for explicit effects of 'local' itself. Your code *is* foulfulling requirements, if I may.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 11:09:36 |
Calls: | 6,645 |
Calls today: | 5 |
Files: | 12,190 |
Messages: | 5,326,616 |