File extension is in extensions list: Difference between revisions

→‎{{header|REXX}}: changed program to reflect new task requirements and examples.
(→‎{{header|zkl}}: mark as "needs updating")
(→‎{{header|REXX}}: changed program to reflect new task requirements and examples.)
Line 621:
 
=={{header|REXX}}==
 
{{update|REXX}}
 
Programming note:   extra code was added to:
:::*   display some error/warning messages
:::*   handle the case of the filename having a ''path''
:::*   handle the case of different types of ''path'' separators
:::*   handle the case of the filename having no file extension
:::*   handle cases of the filename ending in a period
:::*   handle cases of the filename ending in multiple periods
:::*   handle cases of blanks in the filename and/or extension
<lang rexx>/*REXX program displays if a filename has a known extension (as per a list of EXTs).*/
$= 'zip rar 7z gz archive A## tar.bz2'; upper $ /*a list of "allowable" file extensions*/
/*╔═════════════════════════════════════════════════════════════════════╗
parse arg fn /*obtain optional argument from the CL.*/
║ The list of extensions below have blanks encoded as 'ff'x. ║
p=lastpos(@.,= afn) /*finddefine the lastdefault positionfor ofthe a dot in@. namearray.*/
║ The extension pointed to has a 'ff'x after the period ───►──┐ ║
if fn\='' then (which is used to@.1 indicate= a true blank, duestrip(fn) to /*A filename specified? Then use it. */
REXX's use of blanks inelse listsdo; as delimiters) @.1 = "MyData.a##" /* else use list*/
@.2 else _= 'a known'"MyData.tar.Gz"
╚═══════════════════════════════════════════════════════════════↓═════╝*/
@.3 = "MyData.gzip"
extensions= '.bat .cmd .com .dat .dll .exe .ini .jpg .jpeg .log .txt . ys'
@.4 = /* [↑] above would be some file EXTs"MyData. */7z.backup"
parse arg fn @.5 = /*obtain the required argument from CL"MyData...*/"
if fn=='' then exit @.6 = /*No filename specified? Then exit.*/"MyData"
afn=translate( fn, '/', "\") @.7 = /*handle both versions of pathSep"MyData_v1. */0.tar.bz2"
afn=translate(afn, 'ff'x, " ") /*@.8 = ··· and also handle true blanks"MyData_v1. */0.bz2"
end
afn=substr(afn, lastpos('/', afn) +1) /*pick off the filename from the path. */
#=words($)
p=lastpos(., afn) /*find last position of a dot in name.*/
if p==0 | p==length(afn) then do j=1 while @.j\=='' /*no dot, or dot is/*traipse atthrough the end@ list of namefile exts.*/
file=@.j; upper file say/*get 'Filenamea 'filename; fnand "then hasuppercase no extension."it*/
do k=1 for # exit /*search $ list to see if ext is in it.*/
x=. || word($, k); L=length(x) /*construct file extension; get endlength.*/
ft=substr(afn, p) if right(file, L)==x then leave /*pickoffDoes thefile fileTypehas an (aka,allowable fileExt).extension?*/
end /*k*/
say 'ft=' ft
upper ft extensions say right(@.j, 30) ' ' word("false true", 1 + (k<=#) ) /*uppercase a couple ofnay REXXor variablesyea.*/
say 'Filename ' fn "has" _end /*j*/ 'extension.' /*stick a fork in it, we're all done. */</lang>
if wordpos(ft,extensions)==0 then _='an unknown'
{{out|output}}
else _= 'a known'
<pre>
say 'Filename ' fn "has" _ 'extension.' /*stick a fork in it, we're all done. */</lang>
MyData.a## true
MyData.tar.Gz true
MyData.gzip false
MyData.7z.backup false
MyData... false
MyData false
MyData_v1.0.tar.bz2 true
MyData_v1.0.bz2 false
</pre>
 
=={{header|Ruby}}==