Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

RE: RE: Re: Sorting on Section Numbers

by tye (Sage)
on Jul 28, 2000 at 17:15 UTC ( #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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://24841]
and the pool shimmers...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2018-03-25 00:56 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (299 votes). Check out past polls.