Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5956319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:14:23+00:00 2026-05-22T18:14:23+00:00

I’ve read tutorials and perldoc on map, tr and references, but this code is

  • 0

I’ve read tutorials and perldoc on map, tr and references, but this code is a little too advanced for a beginner Perl user like myself.

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 particularly need is: what $_ is referring to (undefined?)

The last line including map does what?

I don’t really understand the $a and $b concepts yet. What are they referring to? The first and next element of @allmatches?

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

Here is my idea:
Maps undefined scalar as reference to an array(which?) simultaneously calling the second element: [1]. It sorts my @allmatches array first by amount of occurrences of “MATCH”‘s and then by alphabet. The second map by making a reference is rough for me (maps do a lot in one step); the tr returns number of times. The second “MATCH” is useless, but why?

Bonus: What could I replace tr/// with to sort by more, like if this was possible: tr/MATCH #\d+// ??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:14:24+00:00Added an answer on May 22, 2026 at 6:14 pm

    Reading it right-to-left (i.e., in the order it gets executed)…

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

    For each element e of @allmatches, this creates a reference to a two-element array whose first element is a number and whose second element is e. The result of the map is an array of these references.

    tr/"MATCH"/"MATCH"/ is counting the number of times the letters M, A, T, C, or H occur in e. (It is technically replacing M by M, A by A, T by T, etc., and counting how many such replacements it made.)

    Actually, it is also counting quote characters, since tr/// is going to process those the same as anything else. This appears to be a bug.

    Anyway, let’s say each of these references refers to an array [n,e], where n is the weird count and e is the original element of @allmatches.

    The “sort” then sorts the array of references, primarily by n (interpreted as a string, not a number; this appears to be another bug) and secondarily by the string e.

    Finally, the outermost “map” extracts the second element (e) from each of the two-element arrays after the sorting is done. So the final result is just to do a bizarre (and I believe, buggy) sort on the elements of @allmatches.

    [Edit: As cjm points out in a comment, this map sort map idiom is called a Schwartzian transform.]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for suggestions for debugging... If you view this site in Firefox or
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.