Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: (Golf) Rangify Array

by japhy (Canon)
on Sep 27, 2001 at 08:14 UTC ( [id://114995]=note: print w/replies, xml ) Need Help??


in reply to (Golf) Rangify Array

From List-to-Range generation... oh, and {$a-$b} sorts the same way that {$a<=>$b} does, in two fewer characters.
# 27 * 3 - 1 = 80 chars sub num2range { #23456789012345678901234567 my$x=join',',sort{$a-$b}@_; $x=~s/(?<!\d)(\d+)(?:,((??{ $++1})))+(?!\d)/$1-$+/g;$x }

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re: Re: (Golf) Rangify Array
by kilinrax (Deacon) on Sep 27, 2001 at 21:00 UTC
    You don't need to make that a non-capturing parenthesis, nor resort to lookahead/lookbehind trickery; a simple word boundary will do. Very nice trick with $+, though ;)
    # 27 + 40 + 2 = 69 dude! sub num2range { #234567890123456789012345678901234567890 my$x=join',',sort{$a-$b}@_; $x=~s/\b(\d+)(,((??{$++1})))+\b/$1-$+/g; $x }
      Hmm, good point. The capturing parens are fine. The reason I had look-ahead/behind was because I was looking at a larger problem which doesn't actually exist since we're dealing with numbers. Good move.

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Log In?
Username:
Password:

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

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

    No recent polls found