[[FrontPage]] * Praat Script Tips [#s823a6ce] #contents ** Emulation of Perl's '''split''' function [#r73a5d35] - The subscripts start at 0. - The last subscript is stored in ''lastSub''. - ''delimiter$'' is specified by a regular expression. string$ = "hoge/piyo/foo/bar" delimiter$ = "/" if index_regex("",delimiter$) = 1 lastSub = length(string$)-1 for i from 0 to lastSub arr$[i] = mid$(string$,i+1,1) endfor else lastSub = 0 arr$[0] = string$ repeat lastValue$ = arr$[lastSub] delimiterPos = index_regex(lastValue$,delimiter$) if delimiterPos <> 0 arr$[lastSub] = left$(lastValue$,delimiterPos-1) lastSub = lastSub+1 arr$[lastSub] = replace_regex$(right$(lastValue$,length(lastValue$)-delimiterPos+1),delimiter$,"",1) endif until delimiterPos = 0 endif clearinfo for i from 0 to lastSub hop$ = arr$[i] printline 'i''tab$''hop$' endfor - Result: 0 hoge 1 piyo 2 foo 3 bar - '''Cf.''' Equivalent Perl Script: $string = "hoge/piyo/foo/bar"; $delimiter = "/"; @arr = split(/$delimiter/,$string); for ( $i = 0 ; $i <= $#arr ; $i++ ) { print "$i\t$arr[$i]\n"; } - See also: -- http://www.fon.hum.uva.nl/praat/manual/Scripting_5_6__Arrays.html ** Get the path separator for your platform [#wa2b03b8] - Get the path separator for your platform, and - Rewrite the path. -- Useful especially on Cygwin. input$ = "c:/foo/bar/hoge.TextGrid" output$ = "c:/foo/bar/result" #input$ = "c:\foo\bar\hoge.TextGrid" #output$ = "c:\foo\bar\result" if windows = 1 pathSep$ = "\" notPathSep$ = "/" elsif macintosh + unix >= 1 pathSep$ = "/" notPathSep$ = "\" else exit Sorry. Unknown platform. endif input$ = replace$(input$,notPathSep$,pathSep$,0) output$ = replace$(output$,notPathSep$,pathSep$,0) clearinfo printline 'input$' printline 'output$' - Result (using Windows Praat on Cygwin): c:\foo\bar\hoge.TextGrid c:\foo\bar\result - See also: -- http://www.fon.hum.uva.nl/praat/manual/Scripting_5_1__Variables.html (for predefined numeric variables) ** Get the path and filename from a string [#m2fcc07d] dir$ = "c:/hoge/piyo/foo.TextGrid" #dir$ = "c:\hoge\piyo\foo.TextGrid" if windows + macintosh + unix = 0 exit Sorry. Unknown platform. endif path$ = left$(dir$,rindex_regex(dir$,"[/\\]")) filename$ = right$(dir$,length(dir$)-rindex_regex(dir$,"[/\\]")) ## if you have the path separator (pathSep$) already, you can write: #path$ = left$(dir$,rindex(dir$,pathSep$)) #filename$ = right$(dir$,length(dir$)-rindex(dir$,pathSep$)) clearinfo printline 'path$' printline 'filename$' - Result: c:/hoge/piyo/ foo.TextGrid ** Upper to Lower Case [#idd10905] str$ = "ABCDEFG" newstr$ = replace_regex$(str$,"[A-Z]","\L&",0) echo 'newstr$' - Result: abcdefg - See also: -- http://uk.groups.yahoo.com/group/praat-users/message/5278 ** Get the tiernumbers from the tiernames [#x5d26030] file$ = "N:\sample\sample.TextGrid" select all Remove Read from file... 'file$' segTierNum = 0 wordTierNum = 0 n = Get number of tiers for i from 1 to n currentTier$ = Get tier name... i if currentTier$ = "Seg" segTierNum = i elsif currentTier$ = "Word" wordTierNum = i endif endfor if segTierNum = 0 exit Seg tier not found in 'file$' elsif wordTierNum = 0 exit Word tier not found in 'file$' endif clearinfo printline Seg tier: 'segTierNum' printline Word tier: 'wordTierNum'