Skip to content

Instantly share code, notes, and snippets.

@matthallamew
Created April 9, 2015 17:03
Show Gist options
  • Save matthallamew/f30d062897dabed3e878 to your computer and use it in GitHub Desktop.
Save matthallamew/f30d062897dabed3e878 to your computer and use it in GitHub Desktop.
Quick script to convert non-standard delimited list into a standard, comma-delimited list.
#!/opt/perl514/bin/perl
############################################
# Example input
# MRKT306; ACCT205/206; MGMT320/375/380/420; FNBK306
#
# Example output
# MRKT306,ACCT205,ACCT206,MGMT320,MGMT375,MGMT380,MGMT420,FNBK306
#
#############################################
$_fileName="$ARGV[0]" || "crsqual.txt";
open(FILE, $_fileName) or die "Could not read from $_fileName!";
while(defined($_ = <FILE>)) {
chomp;
$token = $_;
# First level split delimiter = ;
@data = split(/;/, $token);
$goldCrs = undef;
# Loop through each first level token
foreach my $var (@data){
# Second level split delimiter = /
@data2 = split(/\//, $var);
# Check for more than one element in data array
if(scalar @data2 > 1){
# If more than one, grab the characters off of the first array element
my ($crs) = $data2[0] =~ m/(.*[A-Z])/;
# Loop through each second level token
foreach my $var2 (@data2){
# If token is 1 or more digit and does not contain characters
# append the characters we grabbed above to the element
# and add it to the final output string
if($var2 =~ /\d+/ and $var2 !~ m/(.*[A-Z])/){
$goldCrs .= $crs.$var2." ";
}
# else just add the element to the final output string
else{
$goldCrs .= $var2." ";
}
}
}
# else just add it the element to the final output string
else {
$goldCrs .= $var;
}
}
# Fix final output by replacing spaces with commas in between tokens
$gldCrs = join(',', split(/ */,$goldCrs));
print "$gldCrs\n";
}
close FILE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment