DIALOG dlg; string fieldlist,fieldsel,filename,key,st,st1,experlist,expername,seqstr,charno,charstr,charnames; sequence sq; integer a,i,m,p,charnoint; FILE fp; charset cset; fieldlist="Key "; for i=1 to DbGetFieldCount do fieldlist=fieldlist+DbGetFieldName(i)+" "; fieldsel="Key"; charno="7"; //check there's something selected for i=1 to DbGetEntryCount do { if DbGetSel(DbGetEntryKey(i)) then a=a+1; } if a<1 then { message("Select some entries to export before running this script"); stop; } for i=1 to DbGetExperCount do if DbGetExperClass(i)="CHR" then experlist=experlist+DbGetExperName(i)+" "; expername=experlist;expername=splitstring(expername," "); DlgAddText(dlg,"Select the field to label your samples",20,20,180,15); DlgAddList(dlg,fieldlist,fieldsel,20,40,180,150,"MULTI"); DlgAddText(dlg,"Select the Character Type",220,20,130,15); DlgAddList(dlg,experlist,expername,220,40,130,150,"LIST"); DlgAddText(dlg,"No. of Loci",370,20,100,15); DlgAddEdit(dlg,charno,370,40,80,17); if not(DlgShow(dlg,"Export characters",490,220)) then stop; charnoint = val(charno); if (charnoint>ChrSetGetCount(expername)) then { message("There are not this many loci in the character experiment~nTry again with a lower number"); stop; } filename=DbGetPath+"\export.txt"; if not(FileOpenWrite(fp,filename)) then { message("ERROR: unable to create export file"); stop; } charnames=""; //create string of character names for i=1 to charnoint do { charnames=charnames+","+ChrSetGetChar(expername,i); } FileWrite(fp,charnames); FileWriteline(fp); for i=1 to DbGetEntryCount do if DbGetSel(DbGetEntryKey(i)) then { key=DbGetEntryKey(i); st=fieldsel; while st<>"" do { st1=splitstring(st," "); if st1="key" then FileWrite(fp,key+","); if st1<>"key" then FileWrite(fp,DbGetField(key,st1)+","); } //creates delimited string of char vals ChrLoad(cset,key,expername); charstr=""; p=0; for m =1 to charnoint do { charstr=charstr+str(ChrGetVal(cset,m),0,2); if not(m=charnoint) then {charstr=charstr+",";} } FileWrite(fp,charstr); FileWriteLine(fp); } FileClose(fp); message("Results written to "+DbGetPath+"\export.txt"); execute("notepad.exe "+filename);