File extension is in extensions list: Difference between revisions

Content added Content deleted
(→‎{{header|zkl}}: mark as "needs updating")
(→‎{{header|REXX}}: changed program to reflect new task requirements and examples.)
Line 621: Line 621:


=={{header|REXX}}==
=={{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).*/
<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. ║
@.= /*define the default for the @. array.*/
║ The extension pointed to has a 'ff'x after the period ───►──┐ ║
(which is used to indicate a true blank, due to
if fn\='' then @.1 = strip(fn) /*A filename specified? Then use it. */
REXX's use of blanks in lists as delimiters).
else do; @.1 = "MyData.a##" /* else use list*/
@.2 = "MyData.tar.Gz"
╚═══════════════════════════════════════════════════════════════↓═════╝*/
@.3 = "MyData.gzip"
extensions= '.bat .cmd .com .dat .dll .exe .ini .jpg .jpeg .log .txt . ys'
/* [↑] above would be some file EXTs. */
@.4 = "MyData.7z.backup"
parse arg fn /*obtain the required argument from CL.*/
@.5 = "MyData..."
if fn=='' then exit /*No filename specified? Then exit.*/
@.6 = "MyData"
afn=translate( fn, '/', "\") /*handle both versions of pathSep. */
@.7 = "MyData_v1.0.tar.bz2"
afn=translate(afn, 'ff'x, " ") /* ··· and also handle true blanks. */
@.8 = "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 /*no dot, or dot is at the end of name.*/
do j=1 while @.j\=='' /*traipse through @ list of file exts.*/
say 'Filename ' fn " has no extension."
file=@.j; upper file /*get a filename; and then uppercase it*/
exit
do k=1 for # /*search $ list to see if ext is in it.*/
end
x=. || word($, k); L=length(x) /*construct file extension; get length.*/
ft=substr(afn, p) /*pickoff the fileType (aka, fileExt).*/
if right(file, L)==x then leave /*Does file has an allowable extension?*/
end /*k*/
say 'ft=' ft
upper ft extensions /*uppercase a couple of REXX variables.*/
say right(@.j, 30) ' ' word("false true", 1 + (k<=#) ) /*nay or yea.*/
end /*j*/ /*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}}==
=={{header|Ruby}}==