Click to See Complete Forum and Search --> : Concatenating file name in Perl script


proxops-pete
09-12-2007, 04:54 PM
So a co-worker of mine asked me to write a script/program to rename 150 files! :eek:

I got the gist of it done... however, when I try to put 3 variables together to form a part of a new file name using the following syntax, I can't get it to work...

my $final = "$a1" . "" . "$a2" . "m" . "$d3" . "e";

so if $a1 = peter, $a2 = paul, and $d3 = mary, I want $final to be "peterpaulmmarye". :D Concatenating is done using "." operator I thought... however, with or without quotes around the variables, I keep getting

Global symbol "$a2" requires explicit package name at rename.pl line 33.

error messages... :( Any help would be appreciated...

rock
09-12-2007, 05:26 PM
Have you tried using the 'join' command?
$final = join "", "$a1", "", "$a2", "m", "$d3", "e";

proxops-pete
09-13-2007, 09:53 AM
That's an awesome command! :D
However, it still gives me the same error message... :(

In previous lines, I define it by if statements...

if ($d1 eq '540') {my ($a1) = 'lq'};
if ($d1 eq '680') {my ($a1) = 'mq'};
if ($d1 eq '819') {my ($a1) = 'hq'};

are those wrong? Still clueless... trying to learn Perl is such a pain... but thanks for the help rock. :)

proxops-pete
09-13-2007, 11:46 AM
Ok... turns out I had to declar those two ($a1 and $a2) globally first. :p

Thanks rock, again, for that awesome command "join"!! :D I'd never would have seen that coming!

rock
09-13-2007, 01:15 PM
Glad to be of help! I haven't written any Perl for a year or more, but I knew I did concatenation then and pulled up some of that source -- and there it was. Interestingly, I had no idea "." was an option ;)

JPnyc
09-14-2007, 10:32 AM
Why is the second string null? What's the purpose of that?

proxops-pete
09-14-2007, 10:52 AM
Why is the second string null? What's the purpose of that?

that's because "join" works like "split" in that it uses the first string to join all of the others, and since I didn't want anything else inbetween, null string is the way to go! :)