fad3r wrote:
> Hi,
>
> I have been playing around with two pieces of test code:
>
> $first = "Jim";
> $last = "Smith";
> $fullname = $first . " " . $last;
> print $fullname;
>
> $str1 = "Jim";
> $str2 = "Smith";
> $long_str = "$str1 $str2";
> print $long_str;
>
> Both of these succesfully work. However when I try implementing them
> in my code I get errors. Can anyone let me know what I am doing
> wrong?
>
> print "\nWhere are the files? ";
> $dir = <stdin>;
> chomp ($dir);
>
> opendir (DIRECTORY, $dir) || die "cannot open directory\n";
>
> @files = readdir (DIRECTORY);
> foreach $file (@files) {
> $lc_file = lc($file);
> rename("$dir/$file", "$dir/$lc_file");
> }
> #the above works flawlessly
>
> opendir (DIRECTORY, $dir) || die "cannot open directory\n";
> @files2 = readdir (DIRECTORY);
>
> foreach $file2 (@files2) {
> $can_file = $name $file2;
> rename("$dir/$file", "$dir/$can_file");
>
> That returns:
> D:\Scriptz>lc3.pl
> Scalar found where operator expected at D:\Scriptz\lc3.pl line 22,
> file2"
> (Missing operator before $file2?)
> syntax error at D:\Scriptz\lc3.pl line 22, near "$name $file2"
> Execution of D:\Scriptz\lc3.pl aborted due to compilation errors.
>
> Doing it the other way returns:
>
> Use of uninitialized value in concatenation (.) or string at
> D:\Scriptz\lc3.pl l
> ine 23, <stdin> line 2.
> Use of uninitialized value in concatenation (.) or string at
> D:\Scriptz\lc3.pl l
> ine 23, <stdin> line 2.
> Use of uninitialized value in concatenation (.) or string at
> D:\Scriptz\lc3.pl l
> ine 23, <stdin> line 2.
> Use of uninitialized value in concatenation (.) or string at
> D:\Scriptz\lc3.pl l
> ine 23, <stdin> line 2.
> Use of uninitialized value in concatenation (.) or string at
> D:\Scriptz\lc3.pl l
> ine 23, <stdin> line 2.
>
> Thanks again,
>
> I appreciate people helping me learn.
If you want to learn, then make sure you have the first two lines below on
all your scripts.
use strict;
use warnings;
my $first = "Jim";
my $last = "Smith";
my $fullname = $first . " " . $last;
print $fullname, "\n";
my $str1 = "Jim";
my $str2 = "Smith";
my $long_str = "$str1 $str2";
print $long_str, "\n";
my $name = 'foo'; # you need to define $name
# get the directory we are fixing
print "\nWhere are the files? ";
my $dir = <stdin>;
chomp $dir;
# lowercase all the filenames in dir
opendir DIR, $dir or die "opendir $dir: $!\n";
while (my $file = readdir DIR) {
my $lc_file = lc $file;
if ($file ne $lc_file) {
rename "$dir/$file", "$dir/$lc_file" or
warn "rename '$dir/$file', '$dir/$lc_file': $!";
}
}
closedir DIR;
# prepend $name to all the filenames in dir
# this step could actually be done in the above loop and save
# a step.
opendir DIR, $dir or die "opendir $dir: $!\n";
while (my $file = readdir DIR) {
my $can_file = $name . $file;
if (! -e "$dir/$can_file") {
rename "$dir/$file", "$dir/$can_file" or
warn "'$dir/$file', '$dir/$can_file': $!";
}
}
closedir DIR;
__END__
--
,-/- __ _ _ $Bill Luebkert Mailto:[EMAIL PROTECTED]
(_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED]
/ ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs