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

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 ( #1114774=note: print w/replies, xml ) Need Help??


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

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.
  • Comment on Re^2: How can I access object data in a user defined sort cmp function?
  • Download Code

Replies are listed 'Best First'.
Re^3: How can I access object data in a user defined sort cmp function? (OOP)
by LanX (Sage) on Jan 28, 2015 at 17:24 UTC
    > 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!

Re^3: How can I access object data in a user defined sort cmp function?
by LanX (Sage) on Jan 28, 2015 at 18:54 UTC
    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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1114774]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (1)
As of 2022-01-28 23:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (74 votes). Check out past polls.

    Notices?