[EMAIL PROTECTED] wrote:
>
> I am writing a Perl script that reads some kind of INI file. I cannot use
> Config::INI or install any new modules due to some restriction so am reading
> the whole INI text file one line at a time and storing them into an array.
> Here are what on my INI files:
>
> FTP_USER=FTPUser
> FTP_PASSWORD=FTPUser
> FTP_MODE=binary
> FTP_TARGET_SERVER=FTPSERVER
> FTP_ACTION_DIR=C:\FtpAction
> FTP_LOGPATH=C:\Temp
> NUM_OF_DIRS=2
> DEFAULT_TARGET_DIR=C:\Temp
> SOURCE_DIR1=D:\Study\Perl\MyFtp
> SOURCE_DIR2=D:\Study\Perl
> TARGET_DIR1=C:\Dir1
> TARGET_DIR2=C:\Dir2
>
> Because I cannot install new modules, am not using Net::Ftp as well and
> instead creating an FTP batch file which I then execute as #system "ftp -v -n
> -i -w:8192 -s:$FTP_BATCHFILE";
>
> So far so good. Now I want to change my Perl script so that I can parse the
> path of the source file and whether it matches SOURCE_DIR1 or SOURCE_DIR2 and
> if so use TARGET_DIR1 or TARGET_DIR2 respectively on the FTP Server otherwise
> use DEFAULT_TARGET_DIR.
>
> I am running the script as MyFtp.pl "D:\Study\Perl\Myftp\Test1.JPG", so I
> check if ARGV[0] matches the string of SOURCE_DIR1 and if so use C:\Dir1 as
> the target directory when I create the FTP batch file.
>
> At the moment, I am hardcoding the checks as something like ...
>
> if path = $SOURCE_DIR1 then TARGET_DIR=$TARGET_DIR1
> elif path = $SOURCE_DIR2  TARGET_DIR=$TARGET_DIR2
> else TARGET_DIR=$DEFAULT_TARGET_DIR
>
> Of course, the above is not the program code but am sure the gurus understand
> what I mean. Unfortunately, if I have to add two more directories to check for
> example, SOURCE_DIR3, SOURCE_DIR4 .... SOURCE_DIR[N] and TARGET_DIR3,
> TARGET_DIR4 ... TARGET_DIR[N], then I have to check my Perl script. So, what I
> want to be able to know is if it is possible to load the values of SOURCE_DIR1
> to SOURCE_DIR[N] into an array, and if so, how? I want to know if I can create
> a dynamic variables $SOURCE_DIR$[N] that I can load into the array, I know
> what is  the final [N] since I have NUM_OF_DIRS=2. I am wanting to do
> something that will look like this:
>
>
>    my $count=1;
>    my $source_array=()
>    my $target_array=()
>    while ( $count le $NUM_OF_DIRS )
>    {
>       $source_array[$count]=$SOURCE_DIR$count;
>       $target_array[$count]=$TARGET_DIR$count;
>       #print $count . "\n";
>       #print $regex . "\n";
>       #if ($path =~ m/$regex/) {
>       #  print 'match';
>       #} else {
>       #  print 'no match';
>       #}
>       $count++;
>    }
>
> Then somewhere down the code, I do
>
>    my $count=1;
>    while ( $count le $NUM_OF_DIRS )
>    {
>       if ($path =~ m/$source_array[$count]/) {
>         TARGET_DIR=$target_array[$count];
>       } else {
>         TARGET_DIR=$DEFAULT_TARGET_DIR;
>       }
>       $count++;
>    }
>
> Obviously, this is not working at the moment, can someone on the list please
> advise if what I am trying to achieve is possible or not. If it is possible
> but am not creating or parsing the variables correctly, please advise on how I
> should be doing it. I have no problem evaluating each value of the INI files,
> i.e. I can parse $SOURCE_DIR1, $SOURCE_DIR2, $NUM_OF_DIRS etc., I just want to
> know how I can dynamically create a variable $SOURCE_DIR1 to $SOURCE_DIR[N]
> and store them into an array or perhaps how to check if a variable has been
> defined or not?

Hi.

To implement the equivalent of runtime variable declaration you should use a
hash, so you can write

  $source_array[$count]=$init{"SOURCE_DIR$count"};

and so on.

But hashes will also help you further here. The code below creates the %init
hash from the initialisation data, and derives from that a %target hash which
maps each source directory to its corresponding target, so that
$target{$source_dir}, if it exists, is equal to the required target directory.

I hope this is useful to you without any further comment, as there would be a
lot to explain here if I was to go through it all from scratch. You should read
perldoc perldata if you are unfamiliar with the hash data type.

Cheers,

Rob



use strict;
use warnings;

use File::Basename;

my %init = do {
  local $/;
  my $data = <DATA>;
  $data =~ /[^\s=]+/g;
};

my %target;

foreach my $skey (grep /^SOURCE_DIR/, keys %init) {
  (my $tkey = $skey) =~ s/^SOURCE/TARGET/;
  $target{lc $init{$skey}} = $init{$tkey};
}

my @files = qw(
  D:\Study\Perl\MyFtp\Test1.JPG
  D:\Study\Perl\Test2.JPG
  D:\Study\Test3.JPG
);

foreach my $sfile (@files) {
  my $tdir = $target{lc dirname $sfile} || $init{DEFAULT_TARGET_DIR};
  my $tfile = $tdir . '\\' . basename $sfile;
  print $tfile, "\n";
}


__DATA__
FTP_USER=FTPUser
FTP_PASSWORD=FTPUser
FTP_MODE=binary
FTP_TARGET_SERVER=FTPSERVER
FTP_ACTION_DIR=C:\FtpAction
FTP_LOGPATH=C:\Temp
NUM_OF_DIRS=2
DEFAULT_TARGET_DIR=C:\Temp
SOURCE_DIR1=D:\Study\Perl\MyFtp
SOURCE_DIR2=D:\Study\Perl
TARGET_DIR1=C:\Dir1
TARGET_DIR2=C:\Dir2


OUTPUT

C:\Dir1\Test1.JPG
C:\Dir2\Test2.JPG
C:\Temp\Test3.JPG



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to