On 2/27/07, Ravi Malghan <[EMAIL PROTECTED]> wrote:
Hi: I just can't seem to figure this out.
I am trying to declare two associative array

Just call them hashes, everybody else does.

(%nodeowner and %nodeseverity) within another array
called %SESSION

For example
%nodeowner = ("node1", "john", "node2", "nancy");
%nodeseverity = ("node1", 5, "node2", 10);

It is much more clear to write this as

my %nodeowner = (
   node1 => "john",
   node2 => "nancy"
);
my %nodeseverity = (
   node1 => 5,
   node2 => 10
);

In Perl 5 the '=>' operator is just a fancy "," that treats the word
on the left like a string.  Also note the use of the 'my' keyword to
make these variables lexical in scope.


How do I declare %SESSION containing %nodeowner and
%nodeseverity. And how do I access say the value John
give node1 from the nodeowner array that is in
SESSION?
snip

You can do it like by taking a reference (see perldoc perlref) of each hash:

my %session = (
   nodeseverity => \%nodeseverity,
   nodeowner => \%nodeowner
);

But normally you wouldn't; you would just create %session (avoid all
uppercase variable names, they look like file handles to most Perl
programmers) in a nested manner:

my %session = (
   nodeowner => {
       node1 => "john",
       node2 => "nancy"
   },
   nodeseverity {
       node1 => 5,
       node2 => 10
   }
);

You can then access the values like this

print "$session{nodeower}{node1}\n";

You can nest hashes pretty much as far as you want.  See the Hashes of
Hashes section of perldoc perldsc for more deatailed information.

In any case you should think very carefully about how you want to
structure this data.  Based on the names and type of data you shown it
looks like an array of hashes would be better suited to your needs:

my @session = (
   { owner => 'john', severity => 5 },
   { owner => 'nancy', severity => 10 }
);

By doing it this way you can easily iterate over the nodes by saying

for my $node (@session) {
   print "the owner of this session is $node->{owner} and its
severity is $node->{severity}\n"
}

The issue is your use of parallel keys (node1, node2).  Here is an
example of the code above with your current data structure:

for my $key (sort keys %{$session{nodeowner}}) {
   print "the owner of this session is $session{nodeowner}{$key} and
its severity is $session{nodeseverity}{$key}\n";
}

Not only is it uglier, it is buggy: what if there are more keys in
nodeseverity than in nodeowner?  A safer way (that still uses the hash
of hashes structure instead of the array of hashes) would be to swap
the order of the keys like this:

my %session = (
   node1 => { owner => 'john', severity => 5 },
   node2 => { owner => 'nancy', severity => 10 }
);

The code would then look like this:

for my $key (sort keys %session) {
   print "the owner of this session ($key) is $session{$key}{owner}
and its severity is $session{$key}{severity}\n";
}

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to