Hi Luebkert and Martin,
In the code I wanted to have the hash names to be used as the files names in
the array... , the challenge here is for loading the text file data to
different hashes.
the hash name should be hash_A.txt for the A.txt file and hash_B.txt for the
B.txt file
Also I need to call these hash_A.txt file afterwards.
Not sure if this is possible.
foreach $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*/;
$hash{$server}{location} = $location; # the hash name should
be hash_A.txt for the A.txt file and hash_B.txt for the B.txt file
$hash{$server}{ip} = $ip; # the hash name should be
hash_A.txt for the A.txt file and hash_B.txt for the B.txt file
}
close IN;
}
Thanks
VSR.
On Tue, Aug 30, 2011 at 2:08 AM, Bill Luebkert <[email protected]>wrote:
> 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