Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: How do I easily turn a year array into a string

by eye (Chaplain)
on Mar 04, 2009 at 07:30 UTC ( #748023=note: print w/ replies, xml ) Need Help??


in reply to How do I easily turn a year array into a string

Just to be different (and because I don't like two element ranges):

#!/usr/bin/perl my(@yearArray) = (1992, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2004 +, 2005, 2006, 2007, 2008); my($anchor) = shift(@yearArray); my(@out) = $anchor; while (@yearArray > 1) { my $tmp = shift(@yearArray); push(@out, ($yearArray[0] == $anchor + 2 ? '-' : $tmp)); $anchor = $tmp; } push(@out, @yearArray); my $out = join(', ', @out); $out =~ s/(, -)+, /-/g; print $out, "\n";

This assumes that each element of @yearArray is numeric and that it is sorted in ascending order.


Comment on Re: How do I easily turn a year array into a string
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2015-07-04 17:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls