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} };
}
| [reply] [d/l] |
|
| |
|
| [reply] |
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")
PS: downvoted for lack of indentation, I agree with the Guido comment.
¹) hint "Reduction to a simpler problem"! | [reply] [d/l] [select] |
|
Of course I have tried to use identation, but whenever I type TAB, it takes me off the edit box, so I gave up.
| [reply] |
|
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.
| [reply] |
|
> but whenever I type TAB, it takes me off the edit box
SPACE! Man! ;-)
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!
| [reply] |
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.
| [reply] |
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
|
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} };
}
| [reply] [d/l] |
|
(I came to the conclusion Guido was right about whitespace)
Hilarious! ++
| [reply] |
|
just if hacked directly and uncompiled like in this case, Python would be a muuuuuuuuuch bigger mess.
| [reply] |
|
[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. | [reply] [d/l] [select] |
|
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. | [reply] [d/l] |
|
> 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
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
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 :) | [reply] [d/l] |
|
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.
| [reply] |