Extract file extension: Difference between revisions

→‎{{header|REXX}}: added the REXX language.
(→‎{{header|REXX}}: added the REXX language.)
Line 158:
ext 'file.odd_one'
</lang>
 
=={{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.)
<lang rexx>/*REXX program extracts the (legal) file extension from a file name. */
@. = /*define default value for array.*/
if arg(1)\=='' then @.1 = arg(1) /*use the filename from the C.L. */
else do /*No filename given? Use defaults*/
@.1 = 'picture.jpg'
@.2 = 'http://mywebsite.com/pictures/image.png'
@.3 = 'myuniquefile.longextension'
@.4 = 'IAmAFileWithoutExtension'
@.5 = '/path/to.my/file'
@.6 = 'file.odd_one'
end
 
do j=1 while @.j\==''; $=@.j; x= /*process all of the file names. */
p=lastpos(.,$) /*find last position of a period.*/
if p\==0 then x=substr($,p+1) /*Found? Get the stuff after it.*/
select
when $=='' | right($,1)==. | p==0 then x=
when \datatype(x,'A') then x= /*upp&low letters+digs*/
otherwise nop
end /*select*/
if x=='' then x=' [null]' /*use a better name for a "null".*/
else x=. || x /*prefix extension with a period.*/
say 'file ext='left(x,20) 'for file name='$
end /*j*/
/*stick a fork in it, we're done.*/</lang>
'''output''' using the default inputs:
<pre>
file ext=.jpg for file name=picture.jpg
file ext=.png for file name=http://mywebsite.com/pictures/image.png
file ext=.longextension for file name=myuniquefile.longextension
file ext= [null] for file name=IAmAFileWithoutExtension
file ext= [null] for file name=/path/to.my/file
file ext= [null] for file name=file.odd_one
</pre>