Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re(demerphq): Helping Beginners

by demerphq (Chancellor)
on Nov 01, 2001 at 07:12 UTC ( [id://122490]=note: print w/replies, xml ) Need Help??


in reply to Helping Beginners

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?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-03-28 22:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found