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

Re: Generating ranges of numbers

by hdb (Monsignor)
on Jul 01, 2016 at 08:06 UTC ( [id://1166969]=note: print w/replies, xml ) Need Help??


in reply to Generating ranges of numbers

Another version:

use warnings; use strict; my @input = (-1, 1, 2, 3, 5, 6, 8, 10, 11, 12, 13 ); my @ranges = ( [ $input[0] ] ); for (@input[1..$#input]) { if( $_ == $ranges[-1][-1]+1 ) { $ranges[-1][1] = $_; } else { push @ranges, [ $_ ]; } } $" = ":"; print "@$_\n" for @ranges;

Update: replaced "eq" with "==" above.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-24 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found