
FishMonger
Veteran
/ Moderator
Jul 25, 2009, 6:51 AM
Post #5 of 7
(6868 views)
|
Re: [perllearner] need help on searching a log file with 3 dots
[In reply to]
|
Can't Post
|
|
Perl has at least 3 methods to retrieve the file list. 1) glob operator perldoc -f glob 2) opendir / readdir operators perdoc -f opendir perdoc -f readdir 3) < > diamond operator I'm not sure which perldoc it's in, but try 'perldoc perldata' I generally use the diamond operator. To move or copy a file you'd use the File::Copy module. In your example, you used a regex to find the desired file. That's ok, but the OP didn't give any details of the file name other than the number of dots, which means your regex may not work. The C style for loop initialization is IMO messy. It would be cleaner to do it like this:
for my $i ( 0..$#array ) { Since the destination directory doesn't change, it would be better to move its assignment outside of the loop. Here's a basic script I'd use, but it still needs error handling and bullet proofing.
#!/usr/bin/perl use strict; use warnings; use File::Copy; my $dest_dir = './backup'; foreach my $log_file ( <*.log> ) { my $dots = $log_file =~ tr/.//; if ( $dots == 3 ) { move($log_file, "$dest_dir/$log_file"); } }
|