Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

unique values

4 views
Skip to first unread message

George Bouras

unread,
Feb 23, 2021, 9:26:03 AM2/23/21
to
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 $_;
}

Randal L. Schwartz

unread,
Feb 23, 2021, 10:43:11 AM2/23/21
to
>>>>> "George" == George Bouras <f...@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
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Dart/Flutter consulting, Technical writing, Comedy, etc. etc.
Still trying to think of something clever for the fourth line of this .sig

Eric Pozharski

unread,
Feb 24, 2021, 5:33:27 AM2/24/21
to
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.

--
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom

gamo

unread,
Feb 24, 2021, 7:08:07 AM2/24/21
to
El 23/2/21 a las 16:27, Randal L. Schwartz escribió:
>>>>>> "George" == George Bouras <f...@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";
}








>
> And even *that* could use a comment or two for junior devs.
>
> print "Just another Perl hacker,"; # the original
>


--
http://gamo.sdf-eu.org/
perl -E 'say "Press return to continue";'

Rainer Weikusat

unread,
Feb 24, 2021, 10:03:41 AM2/24/21
to
gamo <ga...@telecable.es> writes:
> El 23/2/21 a las 16:27, Randal L. Schwartz escribió:
>>>>>>> "George" == George Bouras <f...@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.

gamo

unread,
Feb 24, 2021, 12:01:09 PM2/24/21
to
El 24/2/21 a las 16:03, Rainer Weikusat escribió:
You have only to add 'sort' before 'keys', but the OP did not.

George Bouras

unread,
Feb 24, 2021, 1:29:45 PM2/24/21
to
Στις 24/2/2021 10:49 π.μ., ο/η Eric Pozharski έγραψε:
> 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.
>


indeed !
0 new messages