Perl: Can someone explain this code? It includes map, sorting, tr and links. (Modified Schwartz transform)

I read the manuals and perldoc on the map, tr and links, but this code is a bit too advanced for a novice Perl user like me.

print map $_->[1], 
sort {
$a->[0] cmp $b->[0] ##first element of the array
or $a->[1] cmp $b->[1] } 
map [ tr/"MATCH"/"MATCH"/, $_ ], @allmatches; 

So what I especially need is: what does $ _ (undefined?) Mean?

The last line, including the map, does what?

I do not understand the concepts of $ a and $ b yet. What are they referring to? The first and next element of @allmatches?

Also, what do all commas do (after the map)? And if it looks like a Schwartz transform, good, because I don’t understand it yet, despite reading.

:
undefined (?), : [1]. @allmatches "MATCH", . , , ( ); tr . "MATCH" , ?

: tr/// , , : tr/MATCH #\d +//??

+3
3

(.. )...

map [ tr/"MATCH"/"MATCH"/, $_ ], @allmatches;

e @allmatches , , - e. .

tr/"MATCH"/"MATCH"/ , , , , . ( M M, A A, T T .., , .)

, , tr/// , . .

, [n, e], n - , e - @allmatches.

, , n ( , , , , ), -, e.

, "" (e) . , , ( , ) @allmatches.

[Edit: cjm , map sort map .]

+14

; ( ), :

print map  { $_->[1] }
      sort {
              $b->[0] <=> $a->[0]
                      ||
              $a->[1] cmp $b->[1]
           }
      map  { [ tr/MATCH// => $_ ] }
      @allmatches;

:

print map  { $_->{DATA} }
      sort {
              $b->{COUNT} <=> $a->{COUNT}
                          ||
              $a->{DATA}  cmp $b->{DATA}
           }
      map  {
             +{
                COUNT  => tr/MATCH//,
                DATA   => $_,
              }
      } @allmatches;

, , :

print map  {         $$_{DATA}      }
      sort {
              $$b{COUNT} <=> $$a{COUNT}
                          ||
              $$a{DATA}  cmp $$b{DATA}
           }
      map  {
             +{
                  COUNT  => tr/MATCH//,
                  DATA   => $_,
              }
      } @allmatches;

, ? , , , :

  map @allmatches | sort | map | print

,

  print(map(sort(map @allmatches)))

, .

+10

, yuck...

print map $_->[1], 
            sort {
          $a->[0] cmp $b->[0] ##first element of the array
          or $a->[1] cmp $b->[1] } 
      map [ tr/"MATCH"/"MATCH"/, $_ ], @allmatches;

sort .

sort { $a->[0] cmp $b->[0] or $a->[1] cmp $b->[1] } ...an array...

ref, (cmp) refs, (cmp 0), .

, . . . map:

map [ tr/"MATCH"/"MATCH"/, $_ ], @allmatches

, -, no-op, tr/// ; . [: tr/// , MATCH; "block" "expr" map, $_ - - .] @allmatches , . ref, refs; MATCH , .

:

print map $_->[1], ...output from sort...;

$_->[1] .

  • , , @allmatches , (, ) MATCH, MATCH ( ) ..

. - , . (Update: (Schwartzian Transform), " " " " ".)

# Schwartzian Transform: sort by number of letters from MATCH and alphabetically
print map  { $_->[1] } 
      sort { $a->[0] <=> $b->[0] or $a->[1] cmp $b->[1] } 
      map  { [ tr/"MATCH"/"MATCH"/, $_ ] }
      @allmatches;

( .)

You say you are entangled in $aand $b. They are mainly magic variables - parameters of the sorting comparison function. The comparison should return a negative value if it $acompares less than $b, or positive if it $acompares more than $b, or zero if they compare equal ones. They ( $aand $b) are names that are used when two names are required; $_used with map(and grepother list conversion functions) where only one name is required.

+3
source

All Articles