Meghanand Acharekar wrote:
Hello,
Hello,
I have written following perl code to sort file in subfolders according to
regex matching
I don't see any regular expressions in the code you provided?
its giving me following error while compiling the code.
Global symbol "@i" requires explicit package name at sortfiles.pl line 21.
Global symbol "@i" requires explicit package name at sortfiles.pl line 30.
Perl Code.
#!/usr/bin/perl -w
# Sorting file in to subdirs
use integer;
use File::Copy;
You don't use that module anywhere so why include it?
use strict;
my $base_dir="/home/gamesroot/meghanand/Lab1";
my $file_base="games-event.log2010";
my $i=0;
That variable is only used in the next statement so you don't really
need it;
my $j=$i+1;
my @file1="";
my @file2="";
Why are you assigning a string to $file1[0] and $file2[0]? These arrays
don't need to be in file scope, they should be defined inside the loop.
for(my $i=0;$i<6;$i++)
That is usually written as:
for my $i ( 0 .. 5 )
{
`mkdir $base_dir/$j`;
There is no need to call mkdir from the shell:
mkdir "$base_dir/$j" or die "Cannot mkdir '$base_dir/$j' $!";
@file1=`ls
$base_dir/games-event.log2010-[0-9][0-9]-[0-9][0-9]_$i[0-4]*`;
There is no need to call an external program like 'ls' to get a list of
file names, and "$i[0-4]" is interpolated by perl as the fourth element
from the end of the @i array (and "$i[5-9]" refers to the same element
[5-9 equals -4 which is the same as 0-4]) which doesn't exist.
my @file1 = glob
"$base_dir/games-event.log2010-[0-9][0-9]-[0-9][0-9]_${i}[0-4]*";
Or:
my @file1 = glob
"$base_dir/games-event.log2010-[0-9][0-9]-[0-9][0-9]_$i\[0-4]*";
foreach(@file1)
{
chomp($_);
`mv $_ $base_dir/$j`;
There is no need to call an external program to rename files, and if you
use Perl's glob function then you don't have to chomp the file name:
rename $_, "$base_dir/$j" or die "Cannot rename '$_'
to '$base_dir/$j' $!";
}
$j=$j+1;
That is usually written as:
$j += 1;
Or simply:
$j++;
John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/