http://qs321.pair.com?node_id=1114767


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

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.

Replies are listed 'Best First'.
Re^2: How can I access object data in a user defined sort cmp function?
by tkguifan (Scribe) on Jan 28, 2015 at 16:07 UTC
    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!