On Wed, Sep 28, 2011 at 09:33:50AM -0500, 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} That's because you have assigned a reference to an anonymous hash to $Policy which means you use $Policy->{"NAME"} rather than this definition:
%Policy = ( NAME => $PlcyName, ... ); in which case you'd need $Policy{"NAME"}; > > prints the value assigned to $PlcyName.. The problems develop when I try to > access the second level structure.. > I'm not surprised. > 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.. which is trying to give you a clue. What is $CondID set to? $Policy->{INSTNCE} evaluates to a hash. Then you ask Perl to access the value given by the key $CondID in that hash. $CondID is a key to a reference to another anonymous hash. But this time you don't use the '->' to get to the value of the DESCRPT key. >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) > well at least you got to the value - with a bit extra thrown in of course. In your data strucure $Policy->{INSTNCE}, INSTNCE is a key to a hash hence the HASH(...) part of the result. %Instance is also a hash, not a reference to one, so $Instance{$CondID} should evaluate to a reference to the anonymous hash pointed to by $CondID. So you really shouldn't get to the value of $CondID->{DESCRPT} without that '->'. I suspect this result is one of Perl's 'doing the right thing' moments. (Or you've not shown the right code in your email.) It's interpreted $Policy->{INSTNCE} correctly, didn't know where to go with the '->' and then interpreted the $Instance{$ConID} as returning a reference to an anonymous hash so decided you really meant $Instance{$CondID}->{DESCR}. What version of Perl are you using? > > p.s. I need to get this working or the boss has threatened to have it > written in vb script.. Please help.. > ... and how are your vb skills? FWIW I played with the data structures... this is what I came up with to get anything to actually work Note 1. the use of strict and warnings and please use them yourself. 2. the hashes/anonymous hashes have been defined in reverse order to yours, it would not have been possible to run the code under the constraints of (1) above otherwise. Which may indicate your data structure reduction given above is inaccurate. Or that you have not been using strict and warnings otherwise you'd have picked up the issue with the order of declaration/definition of the hashes. 3. The use of 'my'. 4. $CondID has been declared and defined. 5. The results #!/usr/bin/perl use strict; use warnings; my $PlcyName = "name"; my $PlcyDesc = "description"; my $InstDesc = "instant description"; my $Severity = "severity"; my $MsgText = "message text"; my $AutoAct = "autoact"; my $CondID = "fred"; my %PlcyActions = ( START => { SEVRTY => $Severity, TEXT => $MsgText, AUTACTS => $AutoAct, }, CONT => { SEVRTY => $Severity, TEXT => $MsgText, AUTACTS => $AutoAct, }, END => { SEVRTY => $Severity, TEXT => $MsgText, AUTACTS => $AutoAct, }, ); my %Instance = ( $CondID => { DESCRPT => $InstDesc, ACTIONS => {%PlcyActions}, }, ); my $Policy = { NAME => $PlcyName, DESCRPT => $PlcyDesc, INSTNCE => { %Instance }, }; print $Policy->{INSTNCE}."\n"; print $Policy->{INSTNCE}{$CondID}{DESCRPT}."\n"; print $Policy->{INSTNCE}{$CondID}->{DESCRPT}."\n"; print $Policy->{INSTNCE}->{$CondID}{DESCRPT}."\n"; with the results : HASH(0x8582dd8) instant description instant description instant description Regards Lesley -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/