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

RE: RE: Re: Sorting on Section Numbers

by tye (Sage)
on Jul 28, 2000 at 17:15 UTC ( [id://24841]=note: print w/replies, xml ) Need Help??


in reply to RE: Re: Sorting on Section Numbers
in thread Sorting on Section Numbers

You simplified it a lot by dropping $maxdigs. Put that back in and I doubt you'll have saved more than a keystroke or two. :) But thanks for the substr method; I like that.

substr("0000$1",-4) sprintf"%04.4d",$1 substr("0"x$maxdigs.$1,-$maxdigs) sprintf"%0$maxdigs.$maxdigs"."d",$1

When I saw the name chip, I wondered if it was you. Welcome to Perl Monks! I've run into your work many times and have been impressed.

If you know you don't need extra leading zeros, then you can also get away with:

grep{s/(^|\D)0+(\d)/$1$2/g,1} sort grep{s/(\d+)/sprintf"%06.6d",$1/ge,1} @sects;

Man, I shouldn't attempt this much thinking before breakfast.

Replies are listed 'Best First'.
Simpler Section Numbers
by chip (Curate) on Jul 28, 2000 at 22:34 UTC
    You simplified it a lot by dropping $maxdigs.

    It's a fair cop, but society's to blame.

    I appreciate your final no-hash sprintf approach, but modifying temp values in a grep gives me the screaming heebie-jeebies, you know what I mean? I'd much rather use map for that sort of thing. But I can't argue with the fact that it works.... And my Rule #0 is, ``Anything that works is better than anything that doesn't.''

        -- Chip Salzenberg, Free-Floating Agent of Chaos

    PS: Thanks for the welcome. This is a neat site.

      Actually I wrote it as map first and, of course, it didn't work because s// doesn't return the modified string. Breaking the unwritten rule of grep seemed slicker than:

      map {s/.../.../ge; $_}
      which still hides side-effects inside map or:
      map {(my $x= $_)=~s/.../.../ge; $x}
      which just, well, isn't slick. :)

Log In?
Username:
Password:

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

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

    No recent polls found