Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
My next problem is this: map {$_->[0]}. I know that map is operating on $_[0] (I think) but what is this: ->[0] ???.
I think you need to get the idea of refrences to arrays and arrays. Consider
my $array=[]; #$array is a refrence to an anonymous array my @array;
Now if I need to get an element of @array directly I use this syntax:
$array[1];
but if I want to get it through the refrence i have to do
$array->[1];
The -> tells perl to treat the '$array' as the SCALAR with the name 'array' and not as another type with the same name, the [] tells perl that the scalar refers to an array and not a hash. The same thing applies to the variables $_ @_ %_

So in the ST you wrap you data and your keys, one at a time, in anonymous arays (Notice the square brackets in the rightmost map body). Then SORT goes through each of the elements, which are refrences to arrays, and has to dereference them to get at the keys, finally the left most map takes of the anonymous array 'wrapper' and throws it away, returning the sorted list.
BTW Maybe you know this, but for map/sort/grep/join its often a good idea to read them from right to left. (Or my preference from bottom to top)

my @sorted=map {$_->[0]} sort{$a->[1] <=> $b->[1]} map {[$_,keyfunction($_)]} @list; my $nasty=join(",", map {$_->[0]} sort{$a->[1] <=> $b->[1]} map {[$_,keyfunction($_)]} grep {/^\w/} @list );

HTH
Yves

In reply to Re: Re: Sorting strings by demerphq
in thread Sorting strings by rbi

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 a coffee break in the Monastery: (4)
As of 2024-04-24 01:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found