On 8/29/2011 10:40 PM, Vaishak S wrote:
> Hi All,
>
> Do we have any way to get the Dynamic hash names used? I am able to create
> the dynamic hashes, however only gets the dynamic has ref name when
> calling
> directly. Please see below the code..I wanted to compare the duplicates
> records with the .txt files. Not sure if I am doing the right way.
You'll need a better explanation of what you're trying to do to
give a proper response.
This modified version would create a hash of unique servernames
using the first encountered loc/ip and tossing dups (I have no
idea if that's what your trying to do).
use strict;
use warnings;
use Data::Dumper; $Data::Dumper::Indent=1; $Data::Dumper::Sortkeys=1;
my $debug = 0;
# these text files are in csv format
# servername,location,ipaddress from where app is accessed
my @array = ('A.txt', 'B.txt');
my %hash = ();
foreach my $file (@array) {
open IN, "data/$file" or die "open $file: $! ($^E)";
while (<IN>) {
chomp;
next if /^\s*#/; # skip header line
my ($server, $location, $ip) = split /\s*,\s*/;
if (exists $hash{$server}) {
print "Skipping dup server $server in file $file\n";
next;
}
$hash{$server}{location} = $location;
$hash{$server}{ip} = $ip;
}
close IN;
}
# see how your hash looks by setting debug on
print (Data::Dumper->Dump([\%hash], [qw(%hash)])) if $debug;
foreach (sort keys %hash) {
printf "%s => location='%s', ip='%s'\n", $_,
$hash{$_}{location}, $hash{$_}{ip};
}
__END__
A.txt:
# ServerName, location, ipaddress from where app is accessed
server1, location1, 1.2.3.4
server2, location2, 1.2.3.5
server3, location3, 1.2.3.6
server4, location4, 1.2.3.7
B.txt:
# ServerName, location, ipaddress from where app is accessed
server1, location1, 1.2.3.4
server5, location5, 1.2.3.8
server3, location3, 1.2.3.6
server6, location6, 1.2.3.9
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs