How to group in ranges a sorted array of integers in Bash -
in bash i'have sorted integer array like:
array[0]=1 array[1]=2 array[2]=3 array[3]=4 array[4]=7 array[5]=9 array[6]=10 array[7]=13 array[8]=15 array[9]=16 and want obtain output like:
1-4,7,9-10,13,15-16
there simple , fast way it?
i don't know of standard ways of doing it, shouldn't hard write own function it. in lines of:
- save first array elements 'value' 'low'
- iterate on array , save current 'index' 'high'
- when array 'value' differs 'index', print out 'low-high' if not 'low == high', print 'low'
- reset 'low' , 'index' current 'value' , continue
- increase 'index' one
should simple enough pseudo pre-sorted non-empty integer array :)
(sorry formatting, i'm on mac pc keyboard , don't along well.)
Comments
Post a Comment