Hello everyone,
     I think I am getting the hang of using perl, just rough around the
edges.  I was hoping you guys can give me a hand.  Here is my issue. 
>From what I understand using global variables such as my #variable; it
not the most "proper" way of coding.  I ran into a problem that I can't
seem to get around using it. (just because I am a little new at this
programming stuff)

 I have a text file that has fields separated by pipes.  Like so

----------------------<snip of text file>----------------------------

#dir name|ip|hostname|back|port|chK|ver|mon|cl|db|pure|usr
/export6/home213/bob.work|192.168.123.23|bob.com|1|1|1|5|21,22|1|0|1|bob
/export5/home413/chuck.home|192.168.123.201|chuck.com|1|1|1|3|80|1|1|1|chuck     
#/export9/home325/sally.home|192.168.123.56|sally.com|1|1|1|3|80,21|1|1|1|sally
/export1/home425/tom.home|192.168.123.100|tom.com|1|0|1|3|80,21|0|1|1|tom   
The text file has about 1000 entries just like the ones above.

Here is the script I wrote:

----------------------<snip of code>---------------------------------

#!/usr/bin/perl
use strict;
use warnings;
$|++;

use FileHandle;

my $msl_Host;
my %msl_Host;
my $serverList = "/usr/local/account/main-server.list";

&mslSplit($serverList);    # pass the list into the subroutine.

foreach my $key (keys %msl_Host) {
        my $href = $msl_Host{$key};
        next if ($href->{'ignore_flag'} =~ "#");
        print "$href->{'host_name'}\n";
}

sub mslSplit {
     my $msl = new FileHandle "@_", "r" || die $?;
     while (<$msl>) {
            chomp;
           my ($backup_dir, $host_ip, $host_name, $backup_flag,undef) =
split(/\|/) ;
           next if $backup_dir =~ /^#directory name/;
           my ($comment, undef, $group_dir, $host_dir) = split(/\// ,
$backup_dir);
           my $daily_server = substr($group_dir,0,length($group_dir)-2);
           my %hostEntry = (
                  'backup_dir'   => $backup_dir,
                  'host_ip'      => $host_ip,
                  'backup_flag'  => $backup_flag,
                  'group_dir'    => $group_dir,
                  'daily_server' => $daily_server,
                  'host_dir'     => $host_dir,
                  'ignore_flag'  => $comment,
                  'host_name'    => $host_name
            );
            $msl_Host{$host_name} = \%hostEntry;
     }
     $msl->close;
}

-------------------------</end of code>---------------------------------
This works exactly the way I want it.  I can change my print statement
in the foreach loop and print any importatn field that I want.  Ther are
no additional fields I need.

    I was wondering what can I do to clean it up. (Maybe be more
efficient)  I really don't like having:

my $msl_Host;
my %msl_Host;

  declared like that.  But I don't know how to rewrite it so I don't
have to declare it like that.  I kind stumbled across getting this just
to work. Any help would be appreciated.

Thanks, 
--chad



Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to