File extension is in extensions list: Difference between revisions
Content added Content deleted
(→{{header|AWK}}: Add solution for revised problem) |
(→{{header|Tcl}}: revised to meet new criteria and extra credit) |
||
Line 912: | Line 912: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
<lang tcl> |
|||
{{update|Tcl}} |
|||
# This example includes the extra credit. |
|||
<lang tcl># Note that these are already all in lower case |
|||
# With a slight variation, a matching suffix can be identified. |
|||
set exts {".txt" ".gz" ".bat" ".c" ".c++" ".exe" ".pdf"} |
|||
# Note that suffixes with two or more dots (ie a dot in suffix) are checked for each case. |
|||
set filenames { |
|||
# This way, filename.1.txt will match for txt, and filename3.tar.gz.1 will match for tar.gz.1 for example. |
|||
text.txt |
|||
text.TXT |
|||
# Example input data: |
|||
test.tar.gz |
|||
set f_list [list \ |
|||
test/test2.exe |
|||
"MyData.a##" \ |
|||
test\\test2.exe |
|||
MyData.tar.Gz \ |
|||
test |
|||
MyData.gzip \ |
|||
a/b/c\\d/foo |
|||
MyData.7z.backup \ |
|||
foo.c |
|||
"MyData..." \ |
|||
foo.C |
|||
MyData \ |
|||
foo.C++ |
|||
MyData_v1.0.tar.bz2 \ |
|||
foo.c# |
|||
MyData_v1.0.bz2 ] |
|||
foo.zkl |
|||
set suffix_input_list [list zip rar 7z gz archive "A##" tar.bz2 ] |
|||
document.pdf |
|||
# Prefix a dot to any suffix that does not begin with a dot. |
|||
set suffix_list [list ] |
|||
foreach s $suffix_input_list { |
|||
if { [string range $s 0 0] ne "." } { |
|||
set s2 "." |
|||
} else { |
|||
set s2 "" |
|||
} |
|||
append s2 $s |
|||
lappend suffix_list [string tolower $s2] |
|||
} |
} |
||
# Check each filename |
|||
foreach name $filenames { |
|||
foreach filename0 $f_list { |
|||
set ext [file extension $name] |
|||
set filename1 [string tolower [file tail $filename0]] |
|||
set suffix1 [file extension $filename1] |
|||
puts "'$ext' (of $name) is present" |
|||
set file_suffix_list [list $suffix1] |
|||
set filename2 [file rootname $filename1] |
|||
set i 0 |
|||
# i is an infinite loop breaker. In case there is some unforseen case.. |
|||
while { $filename2 ne "" && $filename2 ne $filename1 && $i < 32} { |
|||
# Another suffix is possible |
|||
set suffix2 [file extension $filename2] |
|||
if { $suffix2 ne "" } { |
|||
# found another suffix |
|||
append suffix2 $suffix1 |
|||
lappend file_suffix_list $suffix2 |
|||
} |
|||
set suffix1 $suffix2 |
|||
set filename1 $filename2 |
|||
set filename2 [file rootname $filename2] |
|||
incr i |
|||
} |
|||
set a_suffix_found_p 0 |
|||
foreach file_suffix $file_suffix_list { |
|||
if { $file_suffix in $suffix_list } { |
|||
set a_suffix_found_p 1 |
|||
} |
|||
} |
|||
puts -nonewline "${filename0}\t" |
|||
if { $a_suffix_found_p } { |
|||
puts "true" |
|||
} else { |
} else { |
||
puts "false" |
|||
puts "'$ext' (of $name) is absent" |
|||
} |
} |
||
} |
|||
}</lang> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
MyData.a## true |
|||
'.txt' (of text.txt) is present |
|||
MyData.tar.Gz true |
|||
'.TXT' (of text.TXT) is present |
|||
MyData.gzip false |
|||
'.gz' (of test.tar.gz) is present |
|||
MyData.7z.backup false |
|||
'.exe' (of test/test2.exe) is present |
|||
MyData... false |
|||
'.exe' (of test\test2.exe) is present |
|||
MyData false |
|||
'' (of test) is absent |
|||
MyData_v1.0.tar.bz2 true |
|||
'' (of a/b/c\d/foo) is absent |
|||
MyData_v1.0.bz2 false |
|||
'.c' (of foo.c) is present |
|||
'.C' (of foo.C) is present |
|||
'.C++' (of foo.C++) is present |
|||
'.c#' (of foo.c#) is absent |
|||
'.zkl' (of foo.zkl) is absent |
|||
'.pdf' (of document.pdf) is present |
|||
</pre> |
</pre> |
||