Extract file extension: Difference between revisions

Content deleted Content added
m →‎{{header|Lua}}: (shortened)
m →‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.
Line 857: Line 857:


=={{header|REXX}}==
=={{header|REXX}}==
(Using this paraphrased Rosetta Code task's definition that a legal file extension   ''only''   consists of mixed-case Latin letters and/or decimal digits.)
Using this paraphrased Rosetta Code task's definition that:

<lang rexx>/*REXX program extracts the (legal) file extension from a file name. */
a legal file extension &nbsp; ''only'' &nbsp; consists of mixed-case Latin letters and/or decimal digits.
@.= /*define default value for the @ array.*/
<lang rexx>/*REXX pgm extracts the file extension (defined above from the RC task) from a file name*/
parse arg fID /*obtain any optional arguments from CL*/
if fID\=='' then @.1 = fID /*use the filename from the C.L. */
@.= /*define default value for the @ array.*/
else do /*No filename given? Then use defaults.*/
parse arg fID /*obtain any optional arguments from CL*/
if fID\=='' then @.1 = fID /*use the filename from the C.L. */
else do /*No filename given? Then use defaults.*/
@.1 = 'picture.jpg'
@.1 = 'picture.jpg'
@.2 = 'http://mywebsite.com/pictures/image.png'
@.2 = 'http://mywebsite.com/pictures/image.png'
Line 871: Line 873:
end
end


do j=1 while @.j\==''; x= /*process (all of) the file name(s). */
do j=1 while @.j\==''; x= /*process (all of) the file name(s). */
p=lastpos(.,@.j) /*find the last position of a period. */
p=lastpos(.,@.j) /*find the last position of a period. */
if p\==0 then x=substr(@.j,p+1) /*Found a dot? Then get stuff after it*/
if p\==0 then x=substr(@.j, p+1) /*Found a dot? Then get stuff after it*/
if \datatype(x,'A') then x= /*is it upper/lowercase letters|digits?*/
if \datatype(x, 'A') then x= /*is it upper/lowercase letters|digits?*/
if x=='' then x=" [null]" /*use a better name for a "null". */
if x=='' then x= " [null]" /*use a better name for a "null". */
else x=. || x /*prefix the extension with a period. */
else x= . || x /*prefix the extension with a period. */
say 'file ext='left(x,20) "for file name="@.j
say 'file extension=' left(x, 20) "for file name=" @.j
end /*j*/ /*stick a fork in it, we're all done. */</lang>
end /*j*/
'''output''' &nbsp; when using the default (internal) inputs:
/*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; when using the default inputs:
<pre>
<pre>
file ext=.jpg for file name=picture.jpg
file extension= .jpg for file name= picture.jpg
file ext=.png for file name=http://mywebsite.com/pictures/image.png
file extension= .png for file name= http://mywebsite.com/pictures/image.png
file ext=.longextension for file name=myuniquefile.longextension
file extension= .longextension for file name= myuniquefile.longextension
file ext= [null] for file name=IAmAFileWithoutExtension
file extension= [null] for file name= IAmAFileWithoutExtension
file ext= [null] for file name=/path/to.my/file
file extension= [null] for file name= /path/to.my/file
file ext= [null] for file name=file.odd_one
file extension= [null] for file name= file.odd_one
</pre>
</pre>