Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

nrao wwwxxx, xxxxxdup, procmail change subject header, wwwXxx not20, Wwwxxx.doks sas, linux raid resync after reboot, bind-address mysql multiple, sanibleone xxxx, ftp://192.168.100.100/, www.xxxcon

Links

XODOX
Impressum

#1: Dynamic Hashes

Posted on 2011-08-30 07:40:11 by 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(<FILE>){
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,
<div><br></div><div><span class=3D"Apple-style-span" style=3D"border-collap=
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<br>=A0 =A0=
the dynamic hashes, however only gets the dynamic has ref name when calling=
<br>
=A0 =A0directly. Please see below the code..I wanted to compare the =A0dupl=
icates<br>=A0 =A0records with the .txt files. Not sure if I am doing the ri=
ght way.<br>=A0 =A0---<br>=A0 =A0@array =3D (&quot;A.txt&quot;, &quot;B.txt=
&quot;, &quot;C.txt&quot;);<br>
=A0 =A0# these text files are in csv format<br>=A0 =A0#servername,location,=
ipaddress from where app is accessed<br>=A0 =A0foreach $code (@array){<br>=
=A0 =A0open(FILE, &quot;$code&quot;) || die &quot;Cannot open $code for rea=
ding :$!&quot;;<br>
=A0 =A0while(&lt;FILE&gt;){<br>=A0 =A0next if($_ =3D~ /ServerName/i);<br>=
=A0 =A0$line=3D$_;<br>=A0 =A0my($server,$location,$ip)=3Dsplit/,/,$line;<br=
>=A0 =A0$hash{code}{$server}=3D$line;<br>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0#print $hash{code}{$server};<br>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0print &quot;\n&quot;;<br>
=A0 =A0 =A0 =A0 =A0 =A0undef $server,$location,$ip;<br>=A0 =A0}<br><br>=A0 =
=A0close(FILE);<br>=A0 =A0}<br><br>=A0 =A0print \%hash;<br>=A0 =A0#foreach =
$keyname (keys %hash{&quot;A.txt&quot;}){<br>=A0 =A0 =A0print;<br>=A0 =A0}<=
br><br>Thanks</span></div><div><span class=3D"Apple-style-span" style=3D"bo=
rder-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "=
>VSR</span></div>

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

Report this message

#2: Re: Dynamic Hashes

Posted on 2011-08-30 09:08:46 by 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 (<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
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Report this message

#3: Re: Dynamic Hashes

Posted on 2011-09-01 00:27:56 by 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 (<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 <dbecoll@roadrunner.com>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
>

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

<div>Hi Luebkert and Martin,=A0</div><div><br></div><div><br></div>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<div>
<br></div><div>the hash name should be hash_A.txt for the A.txt file and ha=
sh_B.txt for the B.txt file</div><div>Also I need to call these hash_A.txt =
file afterwards.</div><div>Not sure if this is possible.=A0</div><div><br>
</div><div>foreach $file (@array){</div><div>=A0 =A0 =A0 =A0open IN, &quot;=
data/$file&quot; or die &quot;open $file: $! ($^E)&quot;;</div><div>=A0 =A0=
=A0   while (&lt;IN&gt;) {</div>=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<br>
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0my ($server, $location, $ip) =3D split /\s*,=
\s*/;<br>=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<br>=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<br>
=A0 =A0 =A0 =A0}<div>=A0 =A0 =A0 =A0close IN;</div><div>}</div><div><br></d=
iv><div><br></div><div>Thanks</div><div>VSR.</div><div><br><div class=3D"gm=
ail_quote">On Tue, Aug 30, 2011 at 2:08 AM, Bill Luebkert <span dir=3D"ltr"=
>&lt;<a href=3D"mailto:dbecoll@roadrunner.com">dbecoll@roadrunner.com</a>&g=
t;</span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex;"><div class=3D"im">On 8/29/2011 10:40 PM, Va=
ishak S wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">
Hi All,<br>
<br>
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<br>
=A0 =A0directly. Please see below the code..I wanted to compare the =A0dup=
licates<br>
=A0 =A0records with the .txt files. Not sure if I am doing the right way.<=
br>
</blockquote>
<br></div>
You&#39;ll need a better explanation of what you&#39;re trying to do to<br>
give a proper response.<br>
<br>
This modified version would create a hash of unique servernames<br>
using the first encountered loc/ip and tossing dups (I have no<br>
idea if that&#39;s what your trying to do).<br>
<br>
use strict;<br>
use warnings;<br>
use Data::Dumper; $Data::Dumper::Indent=3D1; $Data::Dumper::Sortkeys=3D1;<b=
r>
<br>
my $debug =3D 0;<div class=3D"im"><br>
<br>
# these text files are in csv format<br>
# servername,location,ipaddress from where app is accessed<br>
<br></div>
my @array =3D (&#39;A.txt&#39;, &#39;B.txt&#39;);<br>
<br>
my %hash =3D ();<br>
foreach my $file (@array) {<br>
<br>
=A0 =A0 =A0 =A0open IN, &quot;data/$file&quot; or die &quot;open $file: $!=
($^E)&quot;;<br>
=A0 =A0 =A0 =A0while (&lt;IN&gt;) {<br>
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0chomp;<br>
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0next if /^\s*#/; =A0 =A0 =A0 =A0# skip head=
er line<br>
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0my ($server, $location, $ip) =3D split /\s*=
,\s*/;<br>
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if (exists $hash{$server}) {<br>
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0print &quot;Skipping dup se=
rver $server in file $file\n&quot;;<br>
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0next;<br>
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}<br>
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$hash{$server}{location} =3D $location;<br>
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$hash{$server}{ip} =3D $ip;<br>
=A0 =A0 =A0 =A0}<br>
=A0 =A0 =A0 =A0close IN;<br>
}<br>
# see how your hash looks by setting debug on<br>
print (Data::Dumper-&gt;Dump([\%hash], [qw(%hash)])) if $debug;<br>
<br>
foreach (sort keys %hash) {<br>
=A0 =A0 =A0 =A0printf &quot;%s =3D&gt; location=3D&#39;%s&#39;, ip=3D&#39;=
%s&#39;\n&quot;, $_,<br>
=A0 =A0 =A0 =A0 =A0$hash{$_}{location}, $hash{$_}{ip};<br>
}<br>
<br>
__END__<br>
<br>
A.txt:<br>
# ServerName, location, ipaddress from where app is accessed<br>
server1, location1, 1.2.3.4<br>
server2, location2, 1.2.3.5<br>
server3, location3, 1.2.3.6<br>
server4, location4, 1.2.3.7<br>
B.txt:<br>
# ServerName, location, ipaddress from where app is accessed<br>
server1, location1, 1.2.3.4<br>
server5, location5, 1.2.3.8<br>
server3, location3, 1.2.3.6<br>
server6, location6, 1.2.3.9<br>
</blockquote></div><br></div>

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

Report this message

#4: Re: Dynamic Hashes

Posted on 2011-09-01 00:55:27 by 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 (<IN>) {
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

Report this message