Dynamic Hashes

Dynamic Hashes

am 30.08.2011 07:40:11 von Vaishak

--===============0430952170==
Content-Type: multipart/alternative; boundary=20cf307cfe96fe23b504abb271bd

--20cf307cfe96fe23b504abb271bd
Content-Type: text/plain; charset=ISO-8859-1

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.
---
@array = ("A.txt", "B.txt", "C.txt");
# these text files are in csv format
#servername,location,ipaddress from where app is accessed
foreach $code (@array){
open(FILE, "$code") || die "Cannot open $code for reading :$!";
while(){
next if($_ =~ /ServerName/i);
$line=$_;
my($server,$location,$ip)=split/,/,$line;
$hash{code}{$server}=$line;
#print $hash{code}{$server};
print "\n";
undef $server,$location,$ip;
}

close(FILE);
}

print \%hash;
#foreach $keyname (keys %hash{"A.txt"}){
print;
}

Thanks
VSR

--20cf307cfe96fe23b504abb271bd
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hi All,


se: collapse; font-family: arial, sans-serif; font-size: 13px; ">Do we have=
any way to get the Dynamic hash names used? I am able to create
=A0 =A0=
the dynamic hashes, however only gets the dynamic has ref name when calling=


=A0 =A0directly. Please see below the code..I wanted to compare the =A0dupl=
icates
=A0 =A0records with the .txt files. Not sure if I am doing the ri=
ght way.
=A0 =A0---
=A0 =A0@array =3D ("A.txt", "B.txt=
", "C.txt");

=A0 =A0# these text files are in csv format
=A0 =A0#servername,location,=
ipaddress from where app is accessed
=A0 =A0foreach $code (@array){
=
=A0 =A0open(FILE, "$code") || die "Cannot open $code for rea=
ding :$!";

=A0 =A0while(<FILE>){
=A0 =A0next if($_ =3D~ /ServerName/i);
=
=A0 =A0$line=3D$_;
=A0 =A0my($server,$location,$ip)=3Dsplit/,/,$line; >=A0 =A0$hash{code}{$server}=3D$line;
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0#print $hash{code}{$server};
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0print "\n";

=A0 =A0 =A0 =A0 =A0 =A0undef $server,$location,$ip;
=A0 =A0}

=A0 =
=A0close(FILE);
=A0 =A0}

=A0 =A0print \%hash;
=A0 =A0#foreach =
$keyname (keys %hash{"A.txt"}){
=A0 =A0 =A0print;
=A0 =A0}<=
br>
Thanks
rder-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "=
>VSR


--20cf307cfe96fe23b504abb271bd--

--===============0430952170==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0430952170==--

Re: Dynamic Hashes

am 30.08.2011 09:08:46 von Bill Luebkert

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 () {
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
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Dynamic Hashes

am 01.09.2011 00:27:56 von Vaishak

--===============1682447961==
Content-Type: multipart/alternative; boundary=20cf307ca394cdb6a304abd4a36a

--20cf307ca394cdb6a304abd4a36a
Content-Type: text/plain; charset=ISO-8859-1

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 () {
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 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 () {
> 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
>

--20cf307ca394cdb6a304abd4a36a
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hi Luebkert and Martin,=A0


In the c=
ode I wanted to have the hash names to be used as the files names in the ar=
ray... , the challenge here is for loading the text file data to different =
hashes.=A0


the hash name should be hash_A.txt for the A.txt file and ha=
sh_B.txt for the B.txt file
Also I need to call these hash_A.txt =
file afterwards.
Not sure if this is possible.=A0


foreach $file (@array){
=A0 =A0 =A0 =A0open IN, "=
data/$file" or die "open $file: $! ($^E)";
=A0 =A0=
=A0   while (<IN>) {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0chomp;<=
br>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0next if /^\s*#/; =A0 =A0 =A0 =A0# skip he=
ader line

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0my ($server, $location, $ip) =3D split /\s*,=
\s*/;
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$hash{$server}{location} =3D $locat=
ion; # the hash name should be hash_A.txt for the A.txt file and hash_B.txt=
for the B.txt file
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$hash{$server}{ip} =
=3D $ip;=A0# the hash name should be hash_A.txt for the A.txt file and hash=
_B.txt for the B.txt file

=A0 =A0 =A0 =A0}
=A0 =A0 =A0 =A0close IN;
}

iv>

Thanks
VSR.

ail_quote">On Tue, Aug 30, 2011 at 2:08 AM, Bill Luebkert ><&g=
t;
wrote:

x #ccc solid;padding-left:1ex;">
On 8/29/2011 10:40 PM, Va=
ishak S wrote:

x #ccc solid;padding-left:1ex">
Hi All,



Do we have any way to get the Dynamic hash names used? I am able to create<=
br>
=A0 =A0the dynamic hashes, however only gets the dynamic has ref name when=
calling

=A0 =A0directly. Please see below the code..I wanted to compare the =A0dup=
licates

=A0 =A0records with the .txt files. Not sure if I am doing the right way.<=
br>



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=3D1; $Data::Dumper::Sortkeys=3D1; r>


my $debug =3D 0;




# these text files are in csv format

# servername,location,ipaddress from where app is accessed



my @array =3D ('A.txt', 'B.txt');



my %hash =3D ();

foreach my $file (@array) {



=A0 =A0 =A0 =A0open IN, "data/$file" or die "open $file: $!=
($^E)";

=A0 =A0 =A0 =A0while (<IN>) {

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0chomp;

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0next if /^\s*#/; =A0 =A0 =A0 =A0# skip head=
er line

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0my ($server, $location, $ip) =3D split /\s*=
,\s*/;

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if (exists $hash{$server}) {

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0print "Skipping dup se=
rver $server in file $file\n";

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0next;

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$hash{$server}{location} =3D $location;

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$hash{$server}{ip} =3D $ip;

=A0 =A0 =A0 =A0}

=A0 =A0 =A0 =A0close IN;

}

# see how your hash looks by setting debug on

print (Data::Dumper->Dump([\%hash], [qw(%hash)])) if $debug;



foreach (sort keys %hash) {

=A0 =A0 =A0 =A0printf "%s =3D> location=3D'%s', ip=3D'=
%s'\n", $_,

=A0 =A0 =A0 =A0 =A0$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




--20cf307ca394cdb6a304abd4a36a--

--===============1682447961==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1682447961==--

Re: Dynamic Hashes

am 01.09.2011 00:55:27 von Bill Luebkert

On 8/31/2011 3:27 PM, Vaishak S wrote:
> 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.

> $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

hash_A.txt is not a legal symbol/name for a hash. I have no idea
why you're trying to do what you are doing - why not explain why
you're doing or what you want to accomplish rather than insisting
on using dynamic naming of hashes.

You could try something like this instead:

use strict;
use warnings;
use Data::Dumper; $Data::Dumper::Indent=1; $Data::Dumper::Sortkeys=1;

# these text files are in csv format
# servername,location,ipaddress from where app is accessed
# my @array = ("A.txt", "B.txt", "C.txt");
my @array = ('A', 'B', 'C');

my %hash_A = (); # hash for group A
my %hash_B = ();
my %hash_C = ();
my $href; # href will point to one of above group hashes at a time

foreach my $file (@array) {

eval "\$href = \\%hash_$file"; # point to right hash
# open the corresponding file (I have them in data dir below me)
open IN, "data/$file.txt" or die "open $file.txt: $! ($^E)";
while () {
chomp;
next if /\s*#/;
my ($server, $location, $ip) = split /\s*,\s*/;
if (exists $href->{$server}) {
print "Skipping dup server $server in file $file\n";
next;
}
$href->{$server}{location} = $location;
$href->{$server}{ip} = $ip;
}
close IN;
}
# print (Data::Dumper->Dump([\%hash_A], [qw(%hash_A)])); # see how your hash looks
# print (Data::Dumper->Dump([\%hash_B], [qw(%hash_B)]));
# print (Data::Dumper->Dump([\%hash_C], [qw(%hash_C)]));

# print out hash for 'A' group
foreach (sort keys %hash_A) {
printf "%s => location='%s', ip='%s'\n", $_,
$hash_A{$_}{location}, $hash_A{$_}{ip};
}

# ...

__END__

output:
server1 => location='location1', ip='1.2.3.4'
server2 => location='location2', ip='1.2.3.5'
server3 => location='location3', ip='1.2.3.6'
server4 => location='location4', ip='1.2.3.7'
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs