in reply to Reading directories and parsing standard names
Solving your problems involves 4 steps:
Hope this helps,,,print get_num('.'); sub get_num{ my $dir = shift; (sort{$b<=>$a} map{/(\d+)/, $1}<$dir/foo_*.bar>)[0] + 1; }
Aziz,,,
Update: The space complexity of this algorithm is O(n) and time complexity is O(nlogn). It might be OK for small number of files but there are better ways for larger number of files. Zaxo's algorithm works the same way but performs worse because it does more regexp matches than the algorithm presented. This algorithm does n matches while the other does an order of nlogn matches.
The better answer is as follows:
As it has O(1) space complexity and O(n) time complexity. Needless to say that this is a somplete solution that requires no special cases. It's also much shorter than my previous example.sub get_num{ my $dir = shift; my $c = 0; /(\d+)/ and $1>$c and $c=$1 while <$dir/foo_*.bar>; return $c+1; }
Enjoy.
Aziz,,,
|
|---|