in reply to Re: multiple dir. grep
in thread multiple dir. grep

note: I am fairly new to perl I have 2 dirs one contains files with sequential filenames ( 12345.dat, 12346.dat, 12347.dat etc..) the other contains similar filenames but with an alpha prefix (A23456.dat, F23457.dat, S23458.dat etc..) I need to create a list that contains the filenames of both dirs -with the newest filenames (higest sequential number regardless of prefix) from both at the top the code i am using (at the moment) for generating my list is:
opendir THEDIR, "$dirpath" || die "cant open $dirpath !"; @allfiles = grep /^(?:158|159)/,readdir THEDIR; closedir THEDIR; foreach $file (sort { ($b) cmp ($a) } @allfiles) { print "$file\n";}
please don't laugh..it took me a LONG time to get that to work. As you can see to make it all go a bit faster i have limited the list to only filenames that start with either 158 or 159. What I need to do is somehow get the contents of the other dir into this list whilst still keeping the newest filenames at the top. ps I'm not asking for a complete script just a few pointers as to how to go about doing it.

Replies are listed 'Best First'.
Re^3: multiple dir. grep
by tlm (Prior) on Apr 06, 2005 at 14:07 UTC

    It's quicker to code it than to explain it:

    use strict; my @dirs = qw(dir1 dir2 dir3); my @files; for my $dir ( @dirs ) { opendir my $dh, $dir or die "opendir failed: $dir ($!)\n"; push @files, grep -f $_, readdir $dh; } my @sorted = map $_->[ 0 ], sort { $b->[ 1 ] <=> $a->[ 1 ] } map [ $_, (/(\d+)/)[ 0 ] ], @files; __END__

    the lowliest monk