Concatenating file name in Perl script

Sharky Forums


Results 1 to 7 of 7

Thread: Concatenating file name in Perl script

  1. #1
    Great White Shark proxops-pete's Avatar
    Join Date
    Feb 2003
    Location
    Houston, we have lift off!
    Posts
    10,317

    Concatenating file name in Perl script

    So a co-worker of mine asked me to write a script/program to rename 150 files!

    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". 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...
    Ivy i5-3570K|ASRock Z77E-ITX|Bitfenix Prodigy
    16 GB Corsair Vengeance DDR3-1600|Antec TruePower Trio 550W
    MSI R6850 PE/OC (860/1100)
    Ivy i7-3770|Intel DZ68DB|ThermalTake V9 BlacX Edition
    32 GB G.Skill Ares DDR3-1866|Corsair AX850
    Zotac 1060 Mini 6GB|Dragonfly 1.5 USB DAC

  2. #2
    NullPointerException rock's Avatar
    Join Date
    Sep 2000
    Location
    York, PA
    Posts
    6,203
    Have you tried using the 'join' command?
    $final = join "", "$a1", "", "$a2", "m", "$d3", "e";

    Open Source is free like a puppy is free.

    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames.

    Understanding Evolution

  3. #3
    Great White Shark proxops-pete's Avatar
    Join Date
    Feb 2003
    Location
    Houston, we have lift off!
    Posts
    10,317
    That's an awesome command!
    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.
    Ivy i5-3570K|ASRock Z77E-ITX|Bitfenix Prodigy
    16 GB Corsair Vengeance DDR3-1600|Antec TruePower Trio 550W
    MSI R6850 PE/OC (860/1100)
    Ivy i7-3770|Intel DZ68DB|ThermalTake V9 BlacX Edition
    32 GB G.Skill Ares DDR3-1866|Corsair AX850
    Zotac 1060 Mini 6GB|Dragonfly 1.5 USB DAC

  4. #4
    Great White Shark proxops-pete's Avatar
    Join Date
    Feb 2003
    Location
    Houston, we have lift off!
    Posts
    10,317
    Ok... turns out I had to declar those two ($a1 and $a2) globally first.

    Thanks rock, again, for that awesome command "join"!! I'd never would have seen that coming!
    Ivy i5-3570K|ASRock Z77E-ITX|Bitfenix Prodigy
    16 GB Corsair Vengeance DDR3-1600|Antec TruePower Trio 550W
    MSI R6850 PE/OC (860/1100)
    Ivy i7-3770|Intel DZ68DB|ThermalTake V9 BlacX Edition
    32 GB G.Skill Ares DDR3-1866|Corsair AX850
    Zotac 1060 Mini 6GB|Dragonfly 1.5 USB DAC

  5. #5
    NullPointerException rock's Avatar
    Join Date
    Sep 2000
    Location
    York, PA
    Posts
    6,203
    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

    Open Source is free like a puppy is free.

    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames.

    Understanding Evolution

  6. #6
    Tiger Shark JPnyc's Avatar
    Join Date
    Jan 2005
    Location
    The only city that really matters
    Posts
    907
    Why is the second string null? What's the purpose of that?

  7. #7
    Great White Shark proxops-pete's Avatar
    Join Date
    Feb 2003
    Location
    Houston, we have lift off!
    Posts
    10,317
    Quote Originally Posted by JPnyc
    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!
    Ivy i5-3570K|ASRock Z77E-ITX|Bitfenix Prodigy
    16 GB Corsair Vengeance DDR3-1600|Antec TruePower Trio 550W
    MSI R6850 PE/OC (860/1100)
    Ivy i7-3770|Intel DZ68DB|ThermalTake V9 BlacX Edition
    32 GB G.Skill Ares DDR3-1866|Corsair AX850
    Zotac 1060 Mini 6GB|Dragonfly 1.5 USB DAC

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •