"George" == George Bouras <[email protected]> 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 <[email protected]> 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 <[email protected]> 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 <[email protected]> writes:
El 23/2/21 a las 16:27, Randal L. Schwartz escribió:
"George" == George Bouras <[email protected]> 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$[email protected]> 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: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 08:26:30 |
| Calls: | 12,100 |
| Files: | 15,003 |
| Messages: | 6,517,952 |