extracting inner key from a hash of hashes

D

doni

I have a doubt regarding extracting the inner key from a hash of
hashes.
Here is my hash structure.

%hash=(
mac-address1=>{
destn_address1, distance1
destn_address2, distance2
destn_address3, distance3
}
mac-address2=>{
destn_address1, distance1
destn_address2, distance2
destn_address3, distance3
}
mac-address3=>{
destn_address1, distance1
destn_address2, distance2
destn_address3, distance3
}
);


I was able to extract the distance portion like this.
$hash{$mac-address1}->{$destn address1}

I want to compare the destination address to see if it matches a
particular destination address. Is there anyway using a for-each loop
or any other means, can I extract the value of destination address.
Can anyone let me know, how can I do this or is there anyway can I do
it.

Thanks,
Sekar
 
U

usenet

doni said:
%hash=(
mac-address1=>{
destn_address1, distance1
destn_address2, distance2

That ain't Perl.

Please either post actual code or actual Data::Dumper output for your
structure.
 
D

doni

David,

Here is the actual Data::Dumper output.

$VAR1 = '01:8e';
$VAR2 = {
'01:86' => 8,
'01:80' => 48,
'08:22' => 22
};
$VAR3 = '01:86';
$VAR4 = {
01:8e' => 8,
01:80' => 34,
'08:22' => 54
};
$VAR5 = '01:80';
$VAR6 = {
'01:8e' => 48,
'01:86' => 34,
'08:22' => 46
};
$VAR7 = '08:22';
$VAR8 = {
'01:8e' => 22,
'01:86' => 54,
'08:22' => 46
};

Thanks,
Sekar
 
P

Paul Lalli

doni said:
I have a doubt regarding extracting the inner key from a hash of
hashes.
Here is my hash structure.

%hash=(
mac-address1=>{
destn_address1, distance1
destn_address2, distance2
destn_address3, distance3
}
mac-address2=>{
destn_address1, distance1
destn_address2, distance2
destn_address3, distance3
}
mac-address3=>{
destn_address1, distance1
destn_address2, distance2
destn_address3, distance3
}
);


I was able to extract the distance portion like this.
$hash{$mac-address1}->{$destn address1}

I want to compare the destination address to see if it matches a
particular destination address. Is there anyway using a for-each loop
or any other means, can I extract the value of destination address.
Can anyone let me know, how can I do this or is there anyway can I do
it.

The answer to your question is "yes", but the real answer is "Are you
sure your structure is what it should be?

ddestn_addressX is the key to a hash. So you use the keys() function
on that hash:

for my $mac_address (keys %hash) {
for my $destn_address (keys %{$mac_address} ) {
if ($destn_address eq $particular_destin_address) {
# do something
}
}
}

Paul Lalli
 
D

David Squire

doni wrote:

[Lesson 2: please don't top-post. Top-posting corrected. Perhaps you
might consider reading the posting guidelines for this group? They are
posted here twice a week.]
David,

Here is the actual Data::Dumper output.

$VAR1 = '01:8e';
$VAR2 = {
'01:86' => 8,
'01:80' => 48,
'08:22' => 22
};
$VAR3 = '01:86';
$VAR4 = {
01:8e' => 8,
01:80' => 34,
'08:22' => 54
};
$VAR5 = '01:80';
$VAR6 = {
'01:8e' => 48,
'01:86' => 34,
'08:22' => 46
};
$VAR7 = '08:22';
$VAR8 = {
'01:8e' => 22,
'01:86' => 54,
'08:22' => 46
};

Ermm... how does this correspond to you original question, which seemed
to be about a single hash that in turn contained other hashes, etc. Here
you show eight separate variables, some of which are plain scalars, and
others which are hash references. I think we're going to need to see
some code.


DS
 
T

Tad McClellan

doni said:
Here is my hash structure.

%hash=(
mac-address1=>{
destn_address1, distance1
destn_address2, distance2
destn_address3, distance3
}

I was able to extract the distance portion like this. ^^^^^^^^^^
$hash{$mac-address1}->{$destn address1}
^ ^ ^
^ ^ ^

No you weren't! You have too many dollar signs and too few underscores.

Post Real Code (and data) if you want Real Help.

Have you seen the Posting Guidelines that are posted here frequently?
 
D

doni

I couldn't conceive of a better way to have a structure as I am new to
this. If there is any better way, can you let me know.

Thanks,
Sekar
 
P

Paul Lalli

doni said:
I couldn't conceive of a better way to have a structure as I am new to
this. If there is any better way, can you let me know.

Please stop top posting. I am not the only person to ask this of you.
Trim the quoted material down to only the relevant bits, and then begin
your reply below it.


There is no way for anyone to tell you what the "right" structure to
use is, because you haven't actually said what your goal is. The only
question you asked related to how to get data out of the structure you
already have.

Paul Lalli
 
P

Paul Lalli

David said:
Ermm... how does this correspond to you original question, which seemed
to be about a single hash that in turn contained other hashes, etc. Here
you show eight separate variables, some of which are plain scalars, and
others which are hash references.

Oh come on. David, you know full well that in this case, the OP simply
passed %hash to Dumper(), rather than \%hash. Let's not be too
snippity, shall we?

Paul Lalli
 
D

doni

Paul,

My actual goal is to find the shortest path between the source and the
destination from the extracted information.

Thanks,
Sekar
 
P

Paul Lalli

doni said:

Bzzt. That's three. You're out. You were asked repeatedly to stop
top posting. You don't care about this simple request, I don't care
about helping you. Bye.

Paul Lalli
 
D

doni

Here is my code snippet. It might be confusing...

while ($line=<TEST>)
{
my @value = split(/\s+/,$line);

### Removing unwanted output and also checking for valid path
###
if (($value[0] =~ m/^\d+/) && ($value[2] eq yes))
{
$upstr_mac[$i] = $value[1];
$dwnstr_mac[$i] = $value[0];
$rssi[$i] = $value[3];
$eval_rssi[$i] = (15 - $value[3])*4 + 8;

if ($i > 0)
{
if (($value[0] eq $upstr_mac[$i-1]) &&
($value[1] eq $mac))
{
$sum_rssi = ($value[3] + $rssi[$i-1])/2;
$eval_rssi[$j] = (15 - $sum_rssi)*4 + 8;
print "$sum_rssi\t$eval_rssi[$j]\n";
$calrssi{$mac}->{$dwnstr_mac[$i]} =
$eval_rssi[$j];
print "$dwnstr_mac[$i]\n";
print "rssi value with 1,$value[0] is:
$eval_rssi[$j]\n";
$j++;
}

elsif (($value[1] eq $dwnstr_mac[$i-1]) && ($value[0] eq $mac))
{
$sum_rssi = ($value[3] + $rssi[$i-1])/2;
$eval_rssi[$j] = (15 - $sum_rssi)*4 + 8;
print "$sum_rssi\t$eval_rssi[$j]\n";
$calrssi{$mac}->{$upstr_mac[$i]} =
$eval_rssi[$j];
print "$upstr_mac[$i]\n";
print "rssi value with 0,$value[1] is:
$eval_rssi[$j]\n";
$j++;
}

elsif (($value[0] eq $mac) && ($upstr_mac[$i-1] eq $mac) &&
($value[1] ne $dwnstr_mac[$i-1]) && ($value[1] ne $upstr_mac[$i+1]))
{
$sum_rssi = $value[3]/2;
$eval_rssi[$j] = (15 - $sum_rssi)*4 + 8;
$calrssi{$mac}->{$upstr_mac[$i]} =
$eval_rssi[$j];
$j++;
}

else
{
$sum_rssi = $value[3]/2;
$eval_rssi[$j] = (15 - $sum_rssi)*4 + 8;
$calrssi{$mac}->{$upstr_mac[$i]} =
$eval_rssi[$j];
$j++;
}

}
}
else
{
if ($value[2] =~ m/^\d+/)
{
$mac = $value[2];
print "MAC address for this part is $mac\n";
print "\n";
}
}
$i++;
}

print Dumper(%calrssi);

Thanks,
Sekar
 
D

David Squire

Paul said:
Oh come on. David, you know full well that in this case, the OP simply
passed %hash to Dumper(), rather than \%hash. Let's not be too
snippity, shall we?

Fair enough. I should take more care not to reveal by being tired and
snippy in the world in the world of usenet :)



DS
 
D

doni

thanks glen, now I got the dumper outputting hash properly.
I tried your dereferencing option and it also worked fine.

Thanks,
Sekar

your my @mac_address1_destinations = keys %{ $hash{mac-address1} };

Thanks,
sekar


Glenn said:
At said:
print Dumper(%calrssi);

The Dumper function takes a list of scalars. If you want to dump a hash
(or array) pass a reference instead:

print Dumper(\%calrssi);

An example:

$ perl -MData::Dumper -e '
%test=(a=>1,b=>{c=>2,d=>3,e=>[4,5,6,7]},f=>"g");
# vvv
print "properly\n", Dumper(\%test), "\n";
print "improperly\n", Dumper(%test), "\n";
# ^^^
'
properly
$VAR1 = {
'a' => 1,
'b' => {
'e' => [
4,
5,
6,
7
],
'c' => 2,
'd' => 3
},
'f' => 'g'
};

improperly
$VAR1 = 'a';
$VAR2 = 1;
$VAR3 = 'b';
$VAR4 = {
'e' => [
4,
5,
6,
7
],
'c' => 2,
'd' => 3
};
$VAR5 = 'f';
$VAR6 = 'g';
 
D

doni

thanks glen, now I got the dumper outputting hash properly.
I tried your dereferencing option and it also worked fine.

Thanks,
Sekar



Glenn said:
At said:
print Dumper(%calrssi);

The Dumper function takes a list of scalars. If you want to dump a hash
(or array) pass a reference instead:

print Dumper(\%calrssi);

An example:

$ perl -MData::Dumper -e '
%test=(a=>1,b=>{c=>2,d=>3,e=>[4,5,6,7]},f=>"g");
# vvv
print "properly\n", Dumper(\%test), "\n";
print "improperly\n", Dumper(%test), "\n";
# ^^^
'
properly
$VAR1 = {
'a' => 1,
'b' => {
'e' => [
4,
5,
6,
7
],
'c' => 2,
'd' => 3
},
'f' => 'g'
};

improperly
$VAR1 = 'a';
$VAR2 = 1;
$VAR3 = 'b';
$VAR4 = {
'e' => [
4,
5,
6,
7
],
'c' => 2,
'd' => 3
};
$VAR5 = 'f';
$VAR6 = 'g';
 
T

Tad McClellan

Glenn Jackman said:
You want to know about dereferencing and the keys function.

my @mac_address1_destinations = keys %{ $hash{mac-address1} };
^
^

And you'll also want to know about quoting hash keys. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top