in reply to Reading directories and parsing standard names

I suspect the problem is in the manner of sorting. If you did a string sort on the array of names, foo_10.bar would come before foo_2.bar. A numeric sort on captured digits should work.

Here is a stab at it:

# needs several foo_<n>.bar { my $re = qr/foo_(\d+)\.bar$/; sub seq_num { $_ = shift; m/$re/; return $1; } } my @files = </dir/to/use/foo_*.bar>; my @sorted_files = sort { seq_num($b) <=> seq_num($a) } @files; my $next = 1 + seq_num($sorted_files[0]); open NEXT, "> /dir/to/use/foo_$next.bar"; # print content to NEXT close(NEXT);

A full solution would special-case @files for 0 or 1 elements.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Reading directories and parsing standard names
by Amoe (Friar) on Aug 11, 2001 at 14:53 UTC
    This works great. Thanks loads :) *credits zaxo*