如何使用哈希将数据库名称映射到数据文件中的服务器名称?

时间:2010-08-05 08:35:26

标签: perl

#!usr/bin/perl
use strict;
use warnings;
my $file_name = "rem.txt";
open(FILE, $file_name);
while (<FILE>) {
    my $line = $_;
    if ($line =~ m/(\w*Svr)\b/) {
        my $server_name;
        $server_name = $1;
        my @arr_servername = $server_name;
        print "@arr_servername\n";
        #print "$server_name\n";
    } elsif ($line =~ m/-[dD]\s*\$\{(.*?)_NAME/) {
        my $database_name;
        $database_name = $1;
        my @arr_dbname = $database_name;
        print "\n-----------------\n";
        print "@arr_dbname\n";
        print "\n----------------\n";
        #print "....$database_name\n";
    }
}

REM.TXT

ewrerfSvr      //(extracted---
 Rate=rpm 
 ID=123 
 RATE=45 
 ADDR=retriveBal 
 Grocer="-r -e ${MAIN_ROOT}/logs/stderr -o ${MAIN_ROOT}/logs/stdout -A --" 
freedonSvr 
 BALFSvr   //to be extracted 
 Rate=rpm1 
 ID=12323 
 RATE=45etf 
 ADDR=retriveBal 
 Grocer="-r -e ${MAIN_ROOT}/logs/stderr -o ${MAIN_ROOT}/logs/stdout -A --" 
freedonSvr -D ${REV_AccountBalance_NAME}"// extracted
Refer
Rate =rpm
ID =234
ADDR = calbal
Grocer="-r -e ${MAIN_ROOT}/logs/stderr -o ${MAIN_ROOT}/logs/stdout -A --" -D ${REV_MAx_NAME}"
REstSvr
Grocer="-r -e ${MAIN_ROOT}/logs/stderr -o ${MAIN_ROOT}/logs/stdout -A --" 
freedonSvr -D ${REV_MinBalance_NAME}"

上面的代码检索文件名和数据库名称。我希望将数据库名称映射到服务器名称。我该怎么做?

我的输出以某种顺序分别检索所有数据库名称,并以其他顺序检索服务器名称。我想使用哈希来存储数据库名称和服务器名称,必须保留顺序。我该怎么做?

执行此操作的主要方法是确定哪个服务器使用哪个数据库。

1 个答案:

答案 0 :(得分:1)

你的意思是:

my %mapping;
...
while (<FILE>) { ....
$mapping{$server_name} = $database_name;