Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: How can I access object data in a user defined sort cmp function? by Anonymous Monk
in thread How can I access object data in a user defined sort cmp function? by tkguifan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-20 13:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found