Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Humanized lists of numbers

by bikeNomad (Priest)
on Jun 23, 2001 at 07:29 UTC ( [id://90924]=note: print w/replies, xml ) Need Help??


in reply to Humanized lists of numbers

Another solution that works with letter ranges as well, without using any regexes:

#!/usr/bin/perl -w use strict; sub human { my (@r, $g, $s, $l); while (@_) { $l = $g = $s = shift; $l = shift while (++$g eq ($_[0] || '')); push(@r, $s eq $l ? $s : "$s-$l"); } @r; } print join(", ", human(1..12, 14..21, 'aa'..'af', 'zz'..'aaf')), "\n"; print join(", ", human(1, 2, 3, 5, 7, 8, 9, 11, 14)), "\n";

Output is:

1-12, 14-21, aa-af, zz-aaf 1-3, 5, 7-9, 11, 14

update: made work with solo items (thanks CharlesClarkson!)

Replies are listed 'Best First'.
Re: Re: Humanized lists of numbers
by CharlesClarkson (Curate) on Jun 24, 2001 at 10:36 UTC
    That's not working for the original list: print join(", ", human(1, 2, 3, 5, 7, 8, 9, 11, 14)), "\n"; prints:1-3, 5-3, 7-9, 11-9, 14-9

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-24 23:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found