Question : Perl - loop using variables if exist

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,
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
#! /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);
}

Answer : Perl - loop using variables if exist

I'd do it more like this:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
#! /usr/bin/perl

use strict;
use warnings;
use File::Copy;
use File::Path;
use File::Basename;

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
);

foreach my $file ( @CVSfiles ) {
    my $dir = dirname($file);
    mkpath($dir) if ! -d $dir;
}
Random Solutions  
 
programming4us programming4us