From 709d184f12ce09cbe3a30b64fc93f901373f43fc Mon Sep 17 00:00:00 2001 From: atothrp Date: Thu, 2 May 2019 17:59:35 -0400 Subject: challenge-006/arpad-toth/perl5/ch-1.sh --- challenge-006/arpad-toth/perl5/ch-1.sh | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 challenge-006/arpad-toth/perl5/ch-1.sh diff --git a/challenge-006/arpad-toth/perl5/ch-1.sh b/challenge-006/arpad-toth/perl5/ch-1.sh new file mode 100644 index 0000000000..ba7a8c24c5 --- /dev/null +++ b/challenge-006/arpad-toth/perl5/ch-1.sh @@ -0,0 +1,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”. +#one liner, runs fine in my rhel 7.6 terminal bash shell +perl -e '@nu=split/,/,shift;$i=0;do{if($nu[$i]+1==$nu[$i+1] && $nu[$i]+2==$nu[$i+2] && $j==0){print "$nu[$i]-";$i++;$j=1}elsif($j==1 && $nu[$i]+1==$nu[$i+1]){$i++}else{print "$nu[$i],";$i++;$j=0}}while($i<=$#nu)' 1,2,3,4,9,10,14,15,16 + +#happy coding :-) -- cgit