On 28/09/2011 15:33, Frank Kleinburg wrote:
Hello list,
I've been playing with perl going back to the 4.x days, mostly simple
scripts to monitor server or application daemons, kick off and manage
backups, or read log files.. While some of these programs are fairly
complicated, none have more than just tickled the more sophisticated
features of perl.. You don't need a Maserati if you're only going to the
store for bread and eggs.. Lately I took on a new project in which I have to
be able to deal with a fairly complicated data set, and I need to get that
fancier car running..
To be able to manipulate the information, I created a simple data structure
of 3 parts.. A top level structure which has a hash containing the second
level structure.. This hash contains additional structures of which one is a
hash, leading to the bottom level structure.. Though what I am using is a
bit more involved, for sake of simplicity consider the structure as follows:
$Policy = {
NAME => $PlcyName,
DESCRPT => $PlcyDesc,
INSTNCE => { %Instance },
};
%Instance = (
$CondID => {
DESCRPT => $InstDesc,
ACTIONS => {%PlcyActions},
},
);
%PlcyActions = (
START => {
SEVRTY => $Severity,
TEXT => $MsgText,
AUTACTS => $AutoAct,
},
CONT => {
SEVRTY => $Severity,
TEXT => $MsgText,
AUTACTS => $AutoAct,
},
END => {
SEVRTY => $Severity,
TEXT => $MsgText,
AUTACTS => $AutoAct,
},
);
I need to be able to write to and read from the structure at any point.. I
can access either of the scalars in the top level fairly easily..
print $Policy->{NAME}
prints the value assigned to $PlcyName.. The problems develop when I try to
access the second level structure..
If want to access the value stored in $InstDesc, I can through
$Instance{$CondID}{DESCRPT}.. However I can't access the $InstDesc scalar
from the top.. If I try to print the obvious (at least to me):
$Policy->{INSTNCE}{$CondID}{DESCRPT}
I get the "use of an uninitialized in concatenation" warning.. If I try to
print the following:
$Policy->{INSTNCE}->$Instance{$CondID}{DESCRPT}
I'd get:
HASH(0x235f34)->BadIncident
(Note: "BadIncident" is current value of $InstDesc)
I've read chapters 8& 9 of the camel book so many times I can't tell you,
and chapter 5 a few times from Randal Schwartz's llama book.. I also done
more Google searches than I care to remember.. So please don't reply with
links to FAQ's or docs if that is all you have to say..
Can someone explain how to access $InstDesc?? Also please explain how I
would access (that is read from or write to) to the $Severity scalar on the
bottom structure..
Thanks in advance.. flk
p.s. I need to get this working or the boss has threatened to have it
written in vb script.. Please help..
I think a lot of your problems arise from declaring separate hashes to
be inserted into higher-level ones. In particular your code will not
even compile if you have
use strict;
use warnings;
at the top, as you must before you bring code to others for help.
For instance, when you write
$Policy = {
NAME => $PlcyName,
DESCRPT => $PlcyDesc,
INSTNCE => { %Instance },
};
you are /copying/ the key/value pairs in %Instance into the the
anonymous hash. At the moment %Instance is presumably empty, so the
value of the INSTNCE element will stay empty whatever you do to the
%Instance hash.
Next you write
%Instance = (
$CondID => {
DESCRPT => $InstDesc,
ACTIONS => {%PlcyActions},
},
);
which, unless you really need %Instance as a separate variable, should
probably be written as
$Policy->{INSTNCE} = {
$CondID => {
DESCRPT => $InstDesc,
ACTIONS => {%PlcyActions},
},
};
and the same applies applies to the ACTIONS element of the hash within that.
Also, I doubt if you are getting the data structure you expect. Inspect
it by using
use Data::Dumper;
print Dumper $Policy;
And I wonder why you sare setting the START, CONT and END entries to the
same values? Perl hashes aren't like database records: you can add
fields when the values are available and there is no need to declare all
the elements that you may need from the start.
HTH,
Rob
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/