File extension is in extensions list: Difference between revisions

(Undo revision 187696 by Siskus (talk) incorrect use of category)
Line 1:
{{draft task}}Given a file name and a list of extensions (including the dot), tell whether the file's extension is in the extensions list. The check should be case insensitive. The file might not have an extension.
 
=={{header|AWK}}==
<lang AWK>
# syntax: GAWK -f FILE_EXTENSION_IS_IN_EXTENSIONS_LIST.AWK
BEGIN {
n = split("txt,gz,bat,c,c++,exe,pdf",arr,",")
for (i=1; i<=n; i++) {
ext_arr[tolower(arr[i])] = ""
}
filenames = "c:,txt,text.txt,text.TXT,test.tar.gz,test/test2.exe,test,foo.c,foo.C,foo.C++,foo.c#,foo.zkl,document.pdf"
n = split(filenames,fn_arr,",")
print(" EXT FILENAME")
for (i=1; i<=n; i++) {
ext = ""
if (fn_arr[i] ~ /\./) {
ext = tolower(fn_arr[i])
while (match(ext,/\./) > 0) {
ext = substr(ext,RSTART+1)
}
}
ans = (ext in ext_arr) ? "true" : "false"
printf("%-5s %-5s %s\n",ans,ext,fn_arr[i])
}
exit(0)
}
</lang>
<p>output:</p>
<pre>
EXT FILENAME
false c:
false txt
true txt text.txt
true txt text.TXT
true gz test.tar.gz
true exe test/test2.exe
false test
true c foo.c
true c foo.C
true c++ foo.C++
false c# foo.c#
false zkl foo.zkl
true pdf document.pdf
</pre>
=={{header|D}}==
<lang d>void main() {
477

edits