Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How can I access object data in a user defined sort cmp function?

by tkguifan (Scribe)
on Jan 28, 2015 at 14:19 UTC ( [id://1114750]=perlquestion: print w/replies, xml ) Need Help??

tkguifan has asked for the wisdom of the Perl Monks concerning the following question:

I want to sort the keys of a hash stored within an object. If I create a user defined cmp function for my sort, it only gets the values $a and $b. Within this cmp function how do I access the $self of the object?
# object $self={ hash_ref=>{item1=>'value1',item2=>'value2'} } # cmp funtion sub byvalue { #instead of $a<=>$b; #something like $self->{hash_ref}->{$a}<=>$self->{hash_ref}->{$b}; #how do I access $self in this function? } # object's sort function sub sort_hash_ref_keys_by_value { my $self=shift; my @keys=sort byvalue keys(%{$self->{hash_ref}}); }

Replies are listed 'Best First'.
Re: How can I access object data in a user defined sort cmp function?
by choroba (Cardinal) on Jan 28, 2015 at 14:28 UTC
    You can create a closure for the object:
    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $self = { hash_ref => { item1 => 12, item2 => 9, }, }; print Dumper(sort_hash_ref_keys_by_value($self)); sub sort_hash_ref_keys_by_value { my $self = shift; my $by_value = sub { $self->{hash_ref}->{$a} <=> $self->{hash_ref}->{$b} }; return sort $by_value keys %{ $self->{hash_ref} }; }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      “Schweee-e-ee-t!”   An extremely elegant solution.

      ++   ++   ++   ...

      And, just for completeness, just for the “technically confused curious” passers-by (however many years from now ...) about “what in the heck a ‘closure’ is,” and with no attempt to steal anyone’s thunder:

      A closure is voodoo an advanced technique that Perl makes very easy.   The local variable named $by_value is made to contain a reference to an anonymous sub which was created within a block of code in which a local variable named $self was “visible” to it, and in which it was known that this $self would continue to exist (with its then-present value) for the duration over which it will be needed.   (Namely:   “the lifetime of sub sort_hash_ref_keys_by_value.”)   This anonymous sub ... a “closure” ... will, rather magically, be able to “see” $self too, and can therefore refer to it in addition to $a and $b.   Each time the sort verb invokes this anonymous function, that function will magically be able to “see” $self, as though the sort verb, itself, were also located in the same block of source-code in which the closure-sub is defined.   The next best thing to being in two places at one time.

      Masterful.   Brilliant.

Re: How can I access object data in a user defined sort cmp function?
by LanX (Saint) on Jan 28, 2015 at 17:12 UTC
    why do you need a custom function byvalue() ?

    Even sorting the keys of a normal hash by value¹ can only be done by somehow tunneling the reference into byvalue() by closure/currying (like choroba showed) or by doing a (schwarzian?) transform of the hash into an array of key/value-tupels.

    Both approaches look like overkill if you can do this straightforwardly:

    my %h = %{$self->{hash_ref}}; my @keys = sort { $h{$a} <=> $h{$b} } keys %h;

    Easy to read, easy to understand.

    So why ?

    DB<100> $self={ hash_ref=>{item1=>'value1',item2=>'value2'} } => { hash_ref => { item1 => "value1", item2 => "value2" } } DB<101> %h=%{$self->{hash_ref}} => ("item1", "value1", "item2", "value2") DB<103> @keys=sort {$h{$a}<=>$h{$b}} keys %h; => ("item1", "item2")

    Cheers Rolf

    PS: Je suis Charlie!

    PS: downvoted for lack of indentation, I agree with the Guido comment.

    ¹) hint "Reduction to a simpler problem"!

      Of course I have tried to use identation, but whenever I type TAB, it takes me off the edit box, so I gave up.
        Don't type code into the text area, copy&paste it from your favourite editor where you can also check its syntax with syntax highlighting and test it by running it.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        > but whenever I type TAB, it takes me off the edit box

        SPACE! Man! ;-)

        Cheers Rolf

        PS: Je suis Charlie!

        PS: Choroba is right it's not feasible to hack several lines without typo, and you can't expect others to fiddle with them.

        But since these are your first posts I should have gone easier on you. Sorry!

Re: How can I access object data in a user defined sort cmp function?
by mr_mischief (Monsignor) on Jan 28, 2015 at 23:15 UTC

    If sorting is something you care about within your class, then don't tell sort how to handle your object. Design a method that sorts your data within the object.

Re: How can I access object data in a user defined sort cmp function?
by Anonymous Monk on Jan 28, 2015 at 14:34 UTC

    (I came to the conclusion Guido was right about whitespace)

    using global... I mean, local our variables. Just like $a and $b.
    use strict; use warnings; sub sort_aux { our $self; $a <=> $b; } sub hash_ref_keys_by_value { local our $self = shift; my @keys = sort by_value keys %{ $self->{hash_ref} }; }
      (I came to the conclusion Guido was right about whitespace)
      Hilarious! ++

      Je suis Charlie.
        just if hacked directly and uncompiled like in this case, Python would be a muuuuuuuuuch bigger mess.

        Cheers Rolf

        PS: Je suis Charlie!

      [deleted: got attached to the wrong reply!!]
Re: How can I access object data in a user defined sort cmp function?
by Anonymous Monk on Jan 28, 2015 at 15:46 UTC

    As I understand your question, you have a hash reference, and you want to sort some keys inside the hash. In your example, you want to get 'item1' and 'item2', in that order.

    If that is so, the solution is simple:

    @keys = sort keys %{ $self->{hash_ref} };

    There is no sort routine, because by default sort() sorts things in ascending order. $self->{hash_ref} is a reference to the hash whose keys you want sorted, %{ $self->{hash_ref} } is the hash itself, and keys %{ $self->{hash_ref} } is the list of keys to be sorted.

    If you need a sort routine in the above, the $a and $b would be the individual keys; so if you wanted a reverse sort you would do

    @keys = sort { $b cmp $a } keys %{ $self->{hash_ref} };

    Note that I did not use the word "object" because your example does not contain an object in the Perl sense of the word. Your '$self' would be an object if it had been blessed; that is, if the code to construct it had been

    $self = bless { hash_ref=>{item1=>'value1',item2=>'value2'} }, 'Some::NameSpace';

    The code to sort the keys would not change, though depending on where that code lives, it might violate encapsulation. On the other hand, Perl's encapsulation model has been described more as "I do not make myself at home in your living room because I was not invited," rather than "I do not make myself at home in your living room because you have a shotgun."

    As for "the $self of an object", there is nothing magic about $self; it is simply a variable like any other. You could use $this, or $me, or anything. To access the internals of an object, you simply need a reference to the object, and it does not matter what that reference is called.

      You misunderstand my question. Here $self is a proper object, blessed. It seemed not too enlightening to list the new function of the object ( it is well known how to do it ). However here it is in full detail:
      Package MyObject; sub new { my $self={ hash_ref=>{key1=>'value1',key2=>'value2'} }; bless $self; return $self; } #global variable to access $self my $sort_self; sub byvalue { $sort_self->{hash_ref}->{$a}<=>$sort_self->{hash_ref}->{$b}; } sub sort_hash_ref_keys_by_value { my $self=shift; #set global variable by hand $sort_self=$self; #now the cmp function knows the object my @keys_sorted_by_value=sort byvalue keys(%{$self->{hash_ref}}); } Packahe Main; my $myobject=new MyObject; $myobject->sort_hash_ref_keys_by_value;
      I want more than simply sorting the keys. Based on the keys I have to look up something within the object based on which I can sort the keys. For this I have to access the object's variables. Currently I create a global variable called $sort_self and I set this manually before calling the user defined cmp function byvalue, but this seems very unelegant.
        > I want more than simply sorting the keys. Based on the keys I have to look up something within the object based on which I can sort the keys. For this I have to access the object's variables. Currently I create a global variable called $sort_self and I set this manually before calling the user defined cmp function byvalue, but this seems very unelegant.

        it is not very elegant, b/c - sorry - you are violating OOP and encapsulation.

        The best design is to add a method %keys=$obj->sort_keys_by_val() which returns your keys.

        No need to tunnel $self artificially cause $self will be the first argument.

        update

        the code for this method can be taken from here

        Cheers Rolf

        PS: Je suis Charlie!

        your code is very hard to read and your real intention hard to guess.

        I wanted to show another way, with one method ->sort combined with various ->by_val , ->by_something but it's impossible to test with the spare information provided.

        Cheers Rolf

        PS: Je suis Charlie!

Re: How can I access object data in a user defined sort cmp function?
by geistberg (Novice) on Jan 29, 2015 at 19:18 UTC
    The <=> can dig into objects and hashes, so you can use something like $a->key->{'other'} <=> $b->key->{'other'}. Proof of concept:
    #/usr/bin/perl use warnings; use strict; use Data::Debug; my @things; push @things, Thing->new() for (1..10); debug @things; my @sorted = sort { $a->key->{'other'} <=> $b->key->{'other'} } @thing +s; debug @sorted; package Thing; sub new { return bless { key => { other => int(rand(42))} }, 'Thing'; +} sub key { return shift->{'key'}; } 1;
    I hope that is helpful :)
      This is not the same thing. You are sorting the objects themselves rather than a hash stored within an object. This is a cheat because now $a and $b are object references, so they speak for themselves. If $a and $b are simply keys of a hash, the sort function has no clue to what object they belong. However it is true, as others pointed out, that if I write the compare function in-line instead of a separate function, the in-line code sees the variables of the sub ( including the $self with which it was called ), so it has access to the object.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1114750]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-23 10:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found