arto (ocehb) wrote,
arto
ocehb

Challenge #1 week 6

#Create a script which takes a list of numbers from command line and 
#print the same in the compact form. For example, 
#if you pass “1,2,3,4,9,10,14,15,16” then it should print the compact form like “1-4,9,10,14-16”.



# perl -le '$n = $ARGV[0]; foreach(@ARGV) { if ($n++ == $_) { push @{$a[$k]},$_; } else { push @{$a[++$k]},$_; $n=$_+1} } END{ print join",", map { "$_->[0]-$_->[-1]" } @a}' 1 2 3 4 7 8 10 11 12 13     
1-4,7-8,10-13
#


Аргументы должны быть уже сортированными.
Tags: #create, #if, #print, perl
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 2 comments