Skip to content

Instantly share code, notes, and snippets.

@zhum
Created January 17, 2020 06:06
Show Gist options
  • Select an option

  • Save zhum/f1a2d05e8af738243dfa19defe6cc4f7 to your computer and use it in GitHub Desktop.

Select an option

Save zhum/f1a2d05e8af738243dfa19defe6cc4f7 to your computer and use it in GitHub Desktop.
Unslurm
def interval prefix, str ,postfix
if /(\d+)-(\d+)/.match str
w="%0#{$1.size}d"
$1.to_i.upto($2.to_i).map {|i| "#{prefix}#{w % i}#{postfix}"}.join ','
else
"#{prefix}#{str}"
end
end
#
# Input: string (e.g. "n[123-125,222],n234")
# Outpout: array (["n123","n124","n125","n222","n234"])
#
def unslurm str
str = ",#{str},"
while /([^\[]*),([^,\[]+)(\[[^\]]+\])([^,]*),(.*)/.match str
start=$1
prefix=$2
m=$3
postfix=$4
fin=$5
list=m[1..-2].split ','
result=list.map { |str| ",#{interval(prefix,str,postfix)}" }.join ','
str="#{start},#{result},#{fin}"
# warn "## #{args}"
end
str.split(',').reject{|x| x.length<1}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment