On Friday 14 May 2010 11:58:36 Meghanand Acharekar wrote:
> Hello,
>
> I have written following perl code to sort file in subfolders according to
> regex matching 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;
> use strict;
>
> my $base_dir="/home/gamesroot/meghanand/Lab1";
> my $file_base="games-event.log2010";
>
> my $i=0;
> my $j=$i+1;
> my @file1="";
> my @file2="";
>
> for(my $i=0;$i<6;$i++)
> {
> `mkdir $base_dir/$j`;
> @file1=`ls
> $base_dir/games-event.log2010-[0-9][0-9]-[0-9][0-9]_$i[0-4]*`;
You should use «${i}» instead of «$i» here, otherwise perl will keep
interpolating the variable through the square brackets. Otherwise:
1. There are portable ways to do file mangement from within perl. See:
* http://perl-begin.org/uses/sys-admin/
* http://perldoc.perl.org/perlfunc.html
2. You shouldn't call `...` in void context. Instead use system(...) (and only
for the things that it needs there.).
3. «for my $i (0 .. 5)» is preferable over the C style for loop.
4. Use names in plural for arrays - not "@file1".
5. Be careful when injecting strings into the shell:
http://community.livejournal.com/shlomif_tech/35301.html
Regards,
Shlomi Fish
> foreach(@file1)
> {
> chomp($_);
> `mv $_ $base_dir/$j`;
> }
>
> $j=$j+1;
> `mkdir $base_dir/$j`;
> @file2=`ls
> $base_dir/games-event.log2010-[0-9][0-9]-[0-9][0-9]_$i[5-9]*`;
> foreach(@file2)
> {
>
> chomp($_);
> `mv $_ $base_dir/$j`;
> }
> $j=$j+1;
> }
>
> Thanks in advanced.
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
List of Portability Libraries - http://shlom.in/port-libs
God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/