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

Re: Humanized lists of numbers

by CharlesClarkson (Curate)
on Jun 24, 2001 at 23:18 UTC ( #91107=note: print w/replies, xml ) Need Help??


in reply to Humanized lists of numbers

Perhaps I have too much time on my hands. This sub allows you to change the separators and handles mixed letters and numbers, 0 and negative numbers. Can be called in scalar or list context.

#!/usr/bin/perl use strict; use warnings; sub human { # range separator defaults to '-' my $range_separator = '-'; # last item separator defaults '' my $last_item_separator = ''; # list separator for scalar context my $list_separator = ', '; if (ref $_[0] eq 'ARRAY') { my $format = shift; $range_separator = $$format[0] if $$format[0]; $last_item_separator = $$format[1] if $$format[1]; $list_separator = $$format[2] if $$format[2]; } # get list; my @array = @_; # load first range my @range = ($array[0], $array[0]); shift @array; # the human readable array my @human; # last item is a dummy, should be less than $array[-1] foreach my $to (@array, $range[0]) { # use autoincrement to get next item my $next = $range[1]; $next++; if ($next eq $to) { # increase range $range[1]++; next; } # use autoincrement to get next item (for testing below) $next = $range[0]; $next++; # add current range to human readable array push @human, $range[1] eq $range[0] ? $range[0] : $range[1] eq $next ? @range : "$range[0]$range_separator$range +[1]"; # load next range @range = ($to, $to); } unless ($last_item_separator eq '') { my $last_item = pop @human; push @human, "$last_item_separator$last_item"; } return wantarray ? @human : join $list_separator, @human; } my @array = (1, 2, 3, 5, 7, 8, 9, 11, 14); print 'Read chapters: ', scalar human(@array), " before friday.\n"; print "Read these chapters by friday:\n", scalar human([' through ', ' +and ', "\n"], @array), "\n\n"; print 'Read pages: ', (join ', ', human(@array)), ".\n\n"; print join(', ', human(0, 2 .. 4, 6, 7, 9, 11 .. 14, 17 .. 19)), "\n"; print join(', ', human(1 .. 19)), "\n"; print scalar human(1 .. 19), "\n\n"; print "Mixed:\n"; @array = (0, 2 .. 4, 6, 7, 9, 'A' .. 'F', 11 .. 14, 'G', 17 .. 20); print join(', ', human([' to '], @array)), "\n"; print scalar human(['-', '& '], @array), "\n\n"; print join(', ', human('aa' .. 'ba', -100 .. 4)), "\n"; print scalar human([' - ', undef, ' and '], 'aa' .. 'ba', '-100' .. 4) +, "\n\n"; __END__
prints:
Read chapters: 1-3, 5, 7-9, 11, 14 before friday. Read these chapters by friday: 1 through 3 5 7 through 9 11 and 14 Read pages: 1-3, 5, 7-9, 11, 14. 0, 2-4, 6, 7, 9, 11-14, 17-19 1-19 1-19 Mixed: 0, 2 to 4, 6, 7, 9, A to F, 11 to 14, G, 17 to 20 0, 2-4, 6, 7, 9, A-F, 11-14, G, & 17-20 aa-ba, -100-4 aa - ba and -100 - 4

HTH,
Charles K. Clarkson

Yep, way too much time

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2023-01-30 11:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?