Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Well, I've seen a lot of excellent replys, much better than anything I could post in terms of teaching, but a few comments. Part of the issue is the person you are dealing with. If they are going to have a hard time with the idea of map then an ST or GRT is not going to be an easy thing to describe. OTOH if the person is receptive to an idea of map then the idea of a transform, sort, transform-back isn't going to be so hard.

Whatever the level of the person, I've found that a lot of the time two or three solutions can be the best. They'll pick the one they are most comfortable with, but at the same time (*hopefully*) be intrigued by the other possibilites. Then you can forward them on to the appropriate documentation and let them play.

The other reason I posted was because I couldnt see why you are doing four steps, instead of three or even what I prefer two. I played around with this for a bit and came up with three variations, all simpler (at least to me).

My first solution was a straight transformation of your two stage prepare with a one stage prepare, and I cheated and lost the call to trim, using m// in list context.

@data = map { $_->[0] } sort { $a->[3] <=> $b->[3] # YY ||$a->[1] <=> $b->[1] # MM ||$a->[2] <=> $b->[2]} # DD map { [ m!^\s*(\D*(\d+)/(\d+)/(\d+)(?: [\d.]+)*)\s*$! ] } @idata; # 0 1MM 2DD 3YY
My next thought was that the date format sucked, and that maybe the sort logic could be simplified in one go, also that I probably would end up splitting it at some point so I might as well return a list of the parts. A bit more complex regex might also be nice.
@data = sort { $a->[1] cmp $b->[1] } # Sort by YYYY/MM/DD map { my @p=m!^\s*([A-Za-z]+)\s+ # alpha word (\d+)/(\d+)/(\d+) # date MM DD YY (?:\s+([\d.]+)) # Substitute Number regex he +re (?:\s+([\d.]+)) # .. (?:\s+([\d.]+)) # .. (?:\s+([\d.]+))!x; # Comments please # Fix the date if this is still in use in 2050... splice @p,1,3,sprintf("%04d/%02d/%02d", ($p[3]>50 ? $p[3]+1900 : $p[3]+2000), @p[1,2]); # it deserves to produce incorrect results, after all # 2 digit dates is madness \@p} # return the fixed array @idata;
But then I decide that I might not want to do that, and I might want it as fast as possible. In which case I wouldn't use an ST but a GRT
@data = map {substr($_,3)} sort #lexicographical representation of the date map { m!^\s*(\D*(\d+)/(\d+)/(\d+)(?: [\d.]+)*)\s*$! && pack ("CCCA*",$4,$2,$3,$1)} @idata;
The point being that these are the kind of ideas that I would probably show an interested colleague if I was asked.

Anyway Ovid thanks for the thought, and for provoking the thoughts you did, (japhy++), I had a good time with this one.

BTW: Im too tired now, but tomorrow I'll update this space with a link to the excellent article on sorting and the Guttman Rosler Transform (do a Super Search until then :)

Yves / DeMerphq
--
Have you registered your Name Space?


In reply to Re(demerphq): Helping Beginners by demerphq
in thread Helping Beginners by Ovid

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-23 17:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found