We help IT Professionals succeed at work.
Get Started

Perl - loop using variables if exist

bt707
bt707 asked
on
329 Views
Last Modified: 2012-05-09
In a script I created, I have a section where I have a long list of files that I checkout from our cvs, then a check to see what files are checked out which will then create a directory for each set of files found.

The sample script below works and will create a the main directory (directory) then a sub directory of (help) and 4 sub directories in (help), such as:

# >ls -l directory/help/
total 8
drwxr-xr-x   2 other  other        512 Jul 23 14:15 change-process
drwxr-xr-x   2 other  other        512 Jul 23 14:15 corporate-identity
drwxr-xr-x   2 other  other        512 Jul 23 14:15 example-clients
drwxr-xr-x   2 other  other        512 Jul 23 14:15 images


This all works ok the way I have it, however, in my script I have a very long list of files from different sub directories, what I want to do is remove all of the if statements I have for creating a directory if that was found and the variable being checked exists.

How can I put the variables I have that I do the check on to see if they exist in a foreach loop or maybe a while loop and then create the directories if needed so I don't have to put in a hundred or so if statements.

Thanks,
#! /usr/bin/perl
use strict;
use File::Copy; 

my $web_dir = 'directory';
if (! -d 'directory.slb.com') {
   mkdir("$web_dir", 0777);
}

# file list to be checked out from CVS
my @CVSfiles = qw(
 development/web-files/help/file.xx
 development/web-files/help/file2.xx
 development/web-files/help/images/file.xx
 development/web-files/help/images/file2.xx
 development/web-files/help/change-process/file.xx
 development/web-files/help/change-process/file2.xx
 development/web-files/help/corporate-identity/file.xx
 development/web-files/help/corporate-identity/file2.xx
 development/web-files/help/example-clients/file.xx
 development/web-files/help/example-clients/file2.xx
);

my $help;
my $images;
my $bulk_ops;
my $change_process;
my $corporate_identity;
my $example_clients;
my $pm_scripts;
my $cgi_scripts;
my $reports;

foreach my $files (@CVSfiles) {
  if ($files =~ m/development\/web-files\/(help)\/.+/) {                  # set $help for directory creation if found
    if ($1 =~ m/help/) {$help = "help"; }
  }
  if ($files =~ m/development\/web-files\/help\/(.+)\/.*/) {           # set $help/sub_directory for directory creation if found
    if ($1 =~ m/images/) {$images = "images"; }
    if ($1 =~ m/bulk-ops/) {$bulk_ops = "bulk-ops"; }
    if ($1 =~ m/change-process/) {$change_process = "change-process"; }
    if ($1 =~ m/corporate-identity/) {$corporate_identity = "corporate-identity"; }
    if ($1 =~ m/example-clients/) {$example_clients = "example-clients"; }
    if ($1 =~ m/cgi-scripts/) {$cgi_scripts = "cgi-scripts"; }
    if ($1 =~ m/reports/) {$reports = "reports"; }
  }
}

##  make sub directories if needed


if (($help) && (! -d "help"))  {
   mkdir("$web_dir/help", 0777);
}

if (($images) && (! -d "help/images"))   {
   mkdir("$web_dir/help/images", 0777);
}

if (($bulk_ops) && (! -d "help/bulk_ops"))   {
   mkdir("$web_dir/help/bulk_ops", 0777);
}

if (($change_process) && (! -d "help/change-process"))   {
   mkdir("$web_dir/help/change-process", 0777);
}

if (($corporate_identity) && (! -d "help/corporate-identity"))   {
   mkdir("$web_dir/help/corporate-identity", 0777);
}

if (($example_clients) && (! -d "help/example-clients"))   {
   mkdir("$web_dir/help/example-clients", 0777);
}

if (($cgi_scripts) && (! -d "help/cgi-scripts"))   {
   mkdir("$web_dir/help/cgi-scripts", 0777);
}

if (($reports) && (! -d "help/reports"))   {
   mkdir("$web_dir/help/reports", 0777);
}

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 2 Answers and 5 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE