Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sort image in a folder

Status
Not open for further replies.

shukui

Technical User
Apr 27, 2001
28
US
Hi there,

How do I sort the following file in a folder? Note that the one or two digits between 'CT' and '3150830758.png' are the keys.

CT03150830758.png
CT13150830758.png
CT23150830758.png
CT33150830758.png
CT43150830758.png
CT53150830758.png
CT103150830758.png
CT113150830758.png
CT123150830758.png
CT133150830758.png
...

Thanks,
 
shukui,
That should be faitly easy. Give the following a try:
Code:
# Open cwd for reading
opendir(DH, '.');

# Sort from character 2 (1st after CT) for a length equal to
# string length - 16 (CT + 3150830758.png)
# Input array is the filtered set of file names retreived by readdir
@sorted = sort {
  substr($a, 2, length ($a) -16) <=> substr($b, 2, length ($b) - 16)
} grep /^CT\w+\.png/, readdir (DH);

closedir(DH);

$&quot;=&quot;\n&quot;;
print &quot;@sorted\n&quot;;
This could be a little cleaner and use dynamic string calcs (i.e. enter a prefix and postfix for match criteria and sort substr length), but should give the general idea.

Hope that helps.
 
Thank you so much usige. It works great! Shukui[2thumbsup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top