Created
January 17, 2020 06:06
-
-
Save zhum/f1a2d05e8af738243dfa19defe6cc4f7 to your computer and use it in GitHub Desktop.
Unslurm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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