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

Re^2: Regex Substring SORT Conundrum

by Polyglot (Chaplain)
on Mar 05, 2015 at 16:45 UTC ( [id://1118921]=note: print w/replies, xml ) Need Help??


in reply to Re: Regex Substring SORT Conundrum
in thread Regex Substring SORT Conundrum

I guess map is where my weakness lies. If only I could understand it. There are some things I've never been able to grasp, it seems. Things like map, grep, and, above all, object references would be at the top of the list. I'm thankful that I seem to have had no trouble understanding regex. Thank you for your pointers, though; I really appreciate all the helpfulness here.

Blessings,

~Polyglot~

Replies are listed 'Best First'.
Re^3: Regex Substring SORT Conundrum
by MidLifeXis (Monsignor) on Mar 05, 2015 at 18:03 UTC

    map is a transforming filter: it takes the right side (after the code block), transforms it according to the code block, and spits the transformed pieces out the other side. Think of it like an assembly line:

    • a bin of widgets is dumped into the hopper at the beginning of the line
    • A whatzit is added to the widget to make a whatever
    • a bin of whatevers is left at the end of the line.

    Does that help explain it at all?

    --MidLifeXis

Re^3: Regex Substring SORT Conundrum
by Ea (Chaplain) on Mar 06, 2015 at 09:51 UTC
    It's definitely worth the effort. Let me see if I can break down the map I gave (and maybe clean it up a bit). Starting from
    map { /^(.*)(?>(?:\s.{1,5}(:\d+)*.*))/; [$_, $sortdata{$1}, $2, $3] }
    you should read it like
    map { my ($book, $chapter, $verse) = /^(.*)(?>(?:\s.{1,5}(:\d+)*.*))/; + [$_, $sortdata{$book}, $chapter, $verse] }
    (I'm taking for granted that your regex works like that) For each element of your list, it
    1. assigns the element to $_
    2. applies the regex and assigns the captures to ($book, $chapter, $verse)
    3. creates an anonymous array [ ] with the original element at index 0 and the other 3 which you'll use in the sort
    4. passes it on to the next process
    so from your first list, you get a list of anonymous arrays for your sort which you can reference using $_->2 for the chapter, etc. Also remember that $a->[1] <=> $b->[1] sorts numerically while $a->[1] cmp $b->[1] sorts alphabetically. The map on the other side of the sort takes the list of sorted anonymous arrays and reduces it to the original elements, now sorted.

    A better explanation of the Transform is in the Modern Perl book

    Sometimes I can think of 6 impossible LDAP attributes before breakfast.
Re^3: Regex Substring SORT Conundrum
by bitingduck (Chaplain) on Mar 05, 2015 at 19:01 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1118921]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-18 10:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found