in reply to Reading directories and parsing standard names

Hello

Solving your problems involves 4 steps:

  1. Get the names of files in the directory matching foo_*.bar.
  2. Map that list to another list of numbers foo_(\d+).bar.
  3. Sort that list numerically in descending order (using spaceship <=>).
  4. Get the first element + 1.
print get_num('.'); sub get_num{ my $dir = shift; (sort{$b<=>$a} map{/(\d+)/, $1}<$dir/foo_*.bar>)[0] + 1; }
Hope this helps,,,

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:

sub get_num{ my $dir = shift; my $c = 0; /(\d+)/ and $1>$c and $c=$1 while <$dir/foo_*.bar>; return $c+1; }
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.

Enjoy.

Aziz,,,