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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

glob sort help needed

Status
Not open for further replies.

billbose

Programmer
Jan 15, 2001
66
US
I have a directory of c source with names:
0.c , 1.c , 2.c, 3.c, 4.c, ..... 9.c, 10.c, 11.c

I do a glob on those files
and do a

foreach $file (@filelist)

print $file


I get the following which is not ordered:

0.c
1.c
10.c
11.c
2.c
3.c
4.c
5.c
..
..
9.c


Whereas I expect it to be

0.c
1.c
2.c
3.c
4.c
5.c
..
..
9.c
10.c
11.c

Can somebody help with the code for sorting it as above.
Thank you. Have a nice day.

 
How about:
[tt]
foreach $file(sort{sprintf('%04s',$a) cmp sprintf('%04s',$b)}(<*>))
{print"$file\n"}
[/tt]
 
it is ordered, but not in numeric order. It's in the default sort order (ASCII order I believe). You could also do something like this:

Code:
my @sorted = map{$_->[0]}
             sort{$a->[1] <=> $b->[1]}
             map{[$_,/^(\d+)/]} @filelist;
print "$_$/" for @sorted;
 
Thanks a lots Gentlemen for these valuable suggestions.
It worked now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top