Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

phpmyadmin fehler rechte unerlaubte mischung, WWWXXXApc, lilo trying to map files from unnamed device 0x0000", Wwwxxxapc, CAM::PDF::Renderer::TextF Fragen, adodb.recordset error '800a0cc1', php save mails as eml, WWWXXXAPC, sqlexpress database file auto-creation error, sqlexpress database file auto-creation error

Links

XODOX
Impressum
Datenschutz

#1: Need help getting data from a structure..

Posted on 2011-09-28 16:33:50 by Frank Kleinburg

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

flk k


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#2: Re: Need help getting data from a structure..

Posted on 2011-09-28 17:56:42 by Brandon McCaig

On Wed, Sep 28, 2011 at 10:33 AM, Frank Kleinburg <fkleinbu@gmail.com> wrot=
e:
>        $Policy =3D {
>                NAME =3D> $PlcyNam=
e,
>                DESCRPT =3D> $Plcy=
Desc,
>                INSTNCE =3D> { %In=
stance },
>        };
>
>        %Instance =3D (
>                $CondID =3D> {
>                     =C2=
=A0  DESCRPT =3D> $InstDesc,
>                     =C2=
=A0  ACTIONS =3D> {%PlcyActions},
>                },
>        );
>
>        %PlcyActions =3D (
>                START =3D> {
>                     =C2=
=A0  SEVRTY  =3D> $Severity,
>                     =C2=
=A0  TEXT    =3D> $MsgText,
>                     =C2=
=A0  AUTACTS =3D> $AutoAct,
>                },
>                 CONT =3D> {
>                     =C2=
=A0  SEVRTY  =3D> $Severity,
>                     =C2=
=A0  TEXT    =3D> $MsgText,
>                     =C2=
=A0  AUTACTS =3D> $AutoAct,
>                },
>                END =3D> {
>                     =C2=
=A0  SEVRTY  =3D> $Severity,
>                     =C2=
=A0  TEXT    =3D> $MsgText,
>                     =C2=
=A0  AUTACTS =3D> $AutoAct,
>                },
>        );

You should always use the 'strict' and 'warnings' pragmas. These
will help you catch subtle errors in your code. They basically
force you to declare your variables and that sort of thing (e.g.,
use 'my' to declare lexically-scoped variables).

use strict;
use warnings;

You appear to have left some parts out of this example, and you
also appear to be assigning these structures in reverse order
(e.g., $Policy->{INSTNCE} is supposed to contain a new hash
reference to a copy of %Instance, which is declared later). Pay
particular attention to the /new/ and /copy/ bits there. The {}
syntax creates a new anonymous hash reference, which you then
copy the contents of %Instance into (or would, if things were
ordered properly). $Policy->{INSTNCE} and $Instance would be two
different data structures that happen to look the same initially.
You might prefer to make them refer to the same thing, in which
case you'd need to use \ to reference the existing hash. See
`perldoc perlref'. For example:

my %Instance =3D (
# ...
);

my $Policy =3D {
NAME =3D> $PlcyName,
DESCRPT =3D> $PlcyDesc,
INSTNCE =3D> \%Instance
};

> 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 t=
o
> print the following:
>
>        $Policy->{INSTNCE}->$Instance{$CondID}{DESCRPT=
}
>
>  I'd get:
>
>        HASH(0x235f34)->BadIncident
>        (Note: "BadIncident" is current value of $Inst=
Desc)

I don't see any concatenation happening so unless it's implicit I
wonder if maybe you're leaving out some significant code. Are you
certain that $CondID is defined and has a sensible value? You can
use Data::Dumper to analyze the data structures and data that you
/actually/ have and compare that to what you /think/ you have.

use Data::Dumper;

print STDERR "CondID=3D$CondID\n";
print STDERR Dumper $Policy;

> 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 t=
he
> bottom structure..

You _seem_ to already grasp the syntax required to dereference a
hash reference and access a hash element so I don't think that
your problem is this. I would recommend you experiment with
Data::Dumper to figure out what the data structure actually is
and see if that helps. You might also benefit from writing out
other variables used (e.g., for keys).

As a side note, your hash key names are pretty arbitrarily
shortened and I find it rather difficult to read and use them.
Some of them are only missing a single letter and often are
side-by-side with longer key names. I would suggest you refactor
the code to use fully descriptive words instead of those
shortened names (unless there's a technical reason why you
aren't).

> p.s. I need to get this working or the boss has threatened to have it
> written in vb script.. Please help..

Dear <insert_deity_here>...! :'(


--=20
Brandon McCaig <http://www.bamccaig.com/> <bamccaig@gmail.com>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <bamccaig@castopulence=
..org>

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#3: Re: Need help getting data from a structure..

Posted on 2011-09-28 19:00:44 by LesleyB

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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#4: Re: Need help getting data from a structure..

Posted on 2011-09-28 21:09:57 by Brandon McCaig

On Wed, Sep 28, 2011 at 1:00 PM, lesleyb <lesleyb@herlug.org.uk> wrote:
> $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 refere=
nce to
> another anonymous hash.  But this time you don't use the '->' to get=
to the
> value of the DESCRPT key.

Only the first -> is needed. After that, Perl knows what to do
without it.

#!/usr/bin/perl

use strict;
use warnings;

my $foo =3D {
bar =3D> {
baz =3D> 'FTW'
}
};

print "Perl $foo->{bar}{baz}!\n";

__END__

> In your data strucure $Policy->{INSTNCE}, INSTNCE is a key to a hash henc=
e 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 anonymou=
s hash
> pointed to by $CondID.  So you really shouldn't get to the value of
> $CondID->{DESCRPT} without that '->'.

Same thing applies here.

#!/usr/bin/perl

use strict;
use warnings;

my %foo =3D (
bar =3D> {
baz =3D> 'FTW'
}
);

print "Perl $foo{bar}{baz}!\n";

__END__

> I suspect this result is one of Perl's 'doing the right thing' moments.

More or less. I think that the reason that it works is that it
isn't ambiguous. At that point in the syntax there's nothing else
that it could mean so Perl just knows that you must be
dereferencing a reference... I think. :P The same applies to
array references and sub references.

> 2.  the hashes/anonymous hashes have been defined in reverse order t=
o yours, it
>    would not have been possible to run the code under the const=
raints 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 otherwis=
e you'd have
>    picked up the issue with the order of declaration/definition=
of the hashes.

Agreed. Technically it could have worked if he had
"forward-declared" the variables earlier in the code, but that
would probably be the wrong thing to do anyway.


--=20
Brandon McCaig <http://www.bamccaig.com/> <bamccaig@gmail.com>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <bamccaig@castopulence=
..org>

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#5: Re: Need help getting data from a structure..

Posted on 2011-09-28 21:27:07 by Brandon McCaig

Frank:

CCing the list since you didn't indicate that this was a private
message. I'm assuming you meant to "reply to all" (which is
probably what you should have done anyway). :)

On Wed, Sep 28, 2011 at 12:55 PM, Frank Kleinburg <fkleinbu@gmail.com> wrot=
e:
> Brandon,
>
> You nailed it.. The one change from "INSTNCE =3D> {%Instance},"
>  to "INSTNCE =3D> \%Instance" and presto..

I'm glad I could help. :)

> And sadly once again, once you have the correct answer, it is
> as obvious as the nose on ones face..
>
> The key "INSTNCE" doesn't contain a hash, but instead refers to
> a hash.. So when It is defined it must be defined as being a
> reference to a hash..
>
> The order and all of that were fine..

The order might be fine with no strict, but I think with strict
enabled it would fail (unless you had already declared the
variables).

> Thanks mucho, and know the first two lines I ever add to a perl
> script are:
>
>        #!/usr/bin/perl -w        =
use strict;

perl -w and the 'warnings' pragma are not entirely the same. The
-w option will enable warnings for the entire program,
*including* any modules that you use, some of which may not be
warnings-compatible (meaning that you could get warnings for
other people's code that actually works fine and isn't your
concern). For this reason I think that it is generally
recommended to use the 'warnings' pragma in each source file
instead.

>
> They are even in my test little scripts..
>
> Thanks millions..
>
> Flk
>
> p.s. As far as the name go, I agree, I've had to cut and paste
> to make sure I spelled the names consistently (thank goodness
> for notepad++)..
>
> I do it so the code looks pretty and to minimize the number of
> lines of code which extend beyond about 132 characters.. This
> way I can print my code with the printer set to portrait mode,
> and save a little paper..

I'm all for limiting line length, but it's give and take. There's
no gain if the shorter line lengths are more difficult to read
and write due to the obscure naming convention. :) I limit my
line lengths to 75 characters (74 actually[1], so there's a
little bit of buffer room before 80), and I have no trouble doing
so with descriptive names. Even in C#, which tends to be quite a
bit more verbose than Perl. You just have to break your lines up
at appropriate word boundaries.


[1] In E-mail I try to remember to limit them to 65 characters so
they hopefully won't wrap for anybody.

Regards,


--=20
Brandon McCaig <http://www.bamccaig.com/> <bamccaig@gmail.com>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <bamccaig@castopulence=
..org>

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#6: Re: Need help getting data from a structure..

Posted on 2011-09-29 02:01:46 by Rob Dixon

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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#7: RE: Need help getting data from a structure..

Posted on 2011-09-29 14:42:43 by RWeidner

> Can someone explain how to access $InstDesc?? Also please=20
> explain how I would access (that is read from or write to)=20
> to the $Severity scalar on the bottom structure..

> Thanks in advance.. flk=20

Sure I can help. (or guide you astray)

#!/usr/bin/perl
use strict;
use warnings;
my %PlcyActions =3D (
START =3D> {
SEVRTY =3D> "Start SEV",
TEXT =3D> "Start TEXT",
AUTACTS =3D> "Start AUTACTS"
},
CONT =3D> {
SEVRTY =3D> "Cont SEV",
TEXT =3D> "Cont TEXT",
AUTACTS =3D> "Cont AUTACTS"
},
END =3D> {
SEVRTY =3D> "END SEV",
TEXT =3D> "END TEXT",
AUTACTS =3D> "END AUTACTS"
},
);

my %Instance =3D (
CONDITION_1 =3D> {
DESCRPT =3D> "Conditions descr",
ACTIONS =3D> \%PlcyActions
},
);

my $Policy =3D {
NAME =3D> "Policy Name",
DESCRPT =3D> "Policy Descr",=20
INSTNCE =3D> \%Instance=20
};

my $testInst =3D $Policy->{INSTNCE};
my $condition =3D $testInst->{CONDITION_1};
print $condition->{DESCRPT} . "\n";

my $action =3D $condition->{ACTIONS};
print $action->{END}->{TEXT} . "\n";

$action->{END}->{TEXT} =3D "NEW END TEXT";
print $action->{END}->{TEXT} . "\n";

## END

It's worth noting, this type of data structure is likely to get hard to man=
age real
fast. (IMO). Without knowing your end goal, my knee jerk reaction to seein=
g code like
this would be to recommend a database to maintain relationships. Other opi=
nions and
approaches may vary.

HTH

--
Ronald Weidner



************************************************************ **********
This e-mail is intended solely for the intended recipient or recipients. If=
this e-mail is addressed to you in error or you otherwise receive this e-m=
ail in error, please advise the sender, do not read, print, forward or save=
this e-mail, and promptly delete and destroy all copies of this e-mail.=20
This email may contain information that is confidential, proprietary or sec=
ret and should be treated as confidential by all recipients. This e-mail ma=
y also be a confidential attorney-client communication, contain attorney wo=
rk product, or otherwise be privileged and exempt from disclosure. If there=
is a confidentiality or non-disclosure agreement or protective order cover=
ing any information contained in this e-mail, such information shall be tre=
ated as confidential and subject to restriction on disclosure and use in ac=
cordance with such agreement or order, and this notice shall constitute ide=
ntification, labeling or marking of such information as confidential, propr=
ietary or secret in accordance with such agreement or order.
The term 'this e-mail' includes any and all attachments.
************************************************************ **********

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#8: RE: Need help getting data from a structure..

Posted on 2011-09-29 15:39:27 by Frank Kleinburg

List,

First I would like to thank all who provided input.. With the help of the
this list, I was able to figure out what I needed to change to get it all
working.. And we have successfully kept VB out (Yippy!)..

Now to the solution (note: I have copied the little test code and its output
below.. Please pardon the misspelt words in the scalar text..)..

Both hashes (%Instance and %PlcyActions) need to be defined as a reference..
So %Instance should have been defined in the Top level structure as:

INSTNCE => \%Instance

This assigns a reference to the hash to the key INSTNCE.. The hash in the
second level structure was redefined in the same way.. This makes perfect
sense, and once I saw that, I knew how to get to the data on all levels..

Now to access it, one additional change was needed.. To access the scalar in
the top level I use:

$Policy->{INSTNCE}{$CondID}->{DESCRPT}

To go all the way to the bottom, I use:

$Policy->{INSTNCE}{$CondID}->{ACTIONS}{START}{SEVRTY}

Again, one the answer was discovered, it all made perfect sense..

Rob, the keys are set to the same values for the test.. I will try the data
dumper.. As I start filling out the structure.. The test code posted is just
the tip of the iceberg so to speak.. The real structure is a lot more
complicated.. This whole piece is actually just a subset of the entire
structure..

Flk k

P.S. My back ground.. Been programming going back to assembler in the 70's..
I always use the "use strict" declarative, and run perl with the "-w"
command line switch.. I do unless I am dealing with code someone else wrote,
and I have'nt had the chance to clean it up (I've redone scores at my
current employer)..

Also I grew up in the days before the internet.. As such I tend to RTFM
(Read The 'Friendly" Manual) long before I go to any list.. I have more
books on perl than you can imagine (as well as C, Python, etc., etc.).. And
then only post after hours of reading FAQ's and posts.. As I said in my
post..

Code follows:

#!perl -w
use strict;

my %Instance;
my %PlcyActions;
my $Policy;
my $PlcyName = "My Most Exelent Good Policy";
my $PlcyDesc = "A good descrition for a policy";
my $Interval = "10h:20m:15s";
my $SourceShortName = "Coda";
my ($CondID, $InstDesc, $Object, $Threshold, $NewOne);
my ($Severity, $MsgText, $AutoAct);

$Policy = {
NAME => $PlcyName,
DESCRPT => $PlcyDesc,
INTERVL => $Interval,
CODA => $SourceShortName,
INSTNCE => \%Instance,
};

$InstDesc = "BadIncident";
$CondID = "12345678";
$CondID = "CONDID_".$CondID;
$Object = "To get this thing to print";
$Threshold = "How much pain can I take";

%Instance = (
$CondID => {
DESCRPT => $InstDesc,
OBJECT => $Object,
THRESHD => $Threshold,
ACTIONS => \%PlcyActions,
},
);

$Severity = "SupperDupperHigh";
$MsgText = "Head for the hills";
$AutoAct = "Shut her down";

%PlcyActions = (
START => {
SEVRTY => $Severity,
TEXT => $MsgText,
AUTACTS => $AutoAct,
},
CONT => {
SEVRTY => $Severity,
TEXT => $MsgText,
AUTACTS => $AutoAct,
},
END => {
SEVRTY => $Severity,
TEXT => $MsgText,
AUTACTS => $AutoAct,
},
);

print "\n-------------------------------------------------\n";
print "Top level access..\n";
print "\t\$Policy->{NAME}:\n\t\t\"$Policy->{NAME}\"..\n";
print "\t\$Policy->{DESCRPT}:\n\t\t\"$Policy->{DESCRPT}\"..\n";
print "\t\$Policy->{INTERVL}:\n\t\t\"$Policy->{INTERVL}\"..\n";
print "\t\$Policy->{CODA}:\n\t\t\"$Policy->{CODA}\"..\n";
print "-------------------------------------------------\n";
print "-------------------------------------------------\n";
print "Second level access..\n";
print "\n\t\$Policy->{INSTNCE}:".
"\n\t\t\"$Policy->{INSTNCE}\"..\n";
print "\n\t\$Policy->{INSTNCE}{\$CondID}:".
"\n\t\t\"$Policy->{INSTNCE}{$CondID}\"..\n";
print "\n\t\$Policy->{INSTNCE}{\$CondID}->{THRESHD}:".
"\n\t\t\"$Policy->{INSTNCE}{$CondID}->{THRESHD}\"..\n";
print "-------------------------------------------------\n";
print "-------------------------------------------------\n";
print "All the way..\n";
print "\n\t\$Policy->{INSTNCE}:".
"\n\t\t\"$Policy->{INSTNCE}\"..\n";
print "\n\t\$Policy->{INSTNCE}{\$CondID}->{ACTIONS}:".
"\n\t\t\"$Policy->{INSTNCE}{$CondID}->{ACTIONS}\"..\n";
print "\n\t\$Policy->{INSTNCE}{\$CondID}->{ACTIONS}{START}{SEVRTY} :".
"\n\t\t\"$Policy->{INSTNCE}{$CondID}->{ACTIONS}{START}{SEVRT Y}\"..\n";
print "\n\t\$Policy->{INSTNCE}{\$CondID}->{ACTIONS}{CONT}{TEXT}:".
"\n\t\t\"$Policy->{INSTNCE}{$CondID}->{ACTIONS}{CONT}{TEXT}\ "..\n";
print "\n\t\$Policy->{INSTNCE}{\$CondID}->{ACTIONS}{END}{AUTACTS}: ".
"\n\t\t\"$Policy->{INSTNCE}{$CondID}->{ACTIONS}{END}{AUTACTS }\"..\n";
print "-------------------------------------------------\n";



Program output:
============================================================ ==========
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\eendh66\Desktop>"test hashs.pl"

-------------------------------------------------
Top level access..
$Policy->{NAME}:
"My Most Exelent Good Policy"..
$Policy->{DESCRPT}:
"A good descrition for a policy"..
$Policy->{INTERVL}:
"10h:20m:15s"..
$Policy->{CODA}:
"Coda"..
-------------------------------------------------
-------------------------------------------------
Second level access..

$Policy->{INSTNCE}:
"HASH(0x182f948)"..

$Policy->{INSTNCE}{$CondID}:
"HASH(0x184af40)"..

$Policy->{INSTNCE}{$CondID}->{THRESHD}:
"How much pain can I take"..
-------------------------------------------------
-------------------------------------------------
All the way..

$Policy->{INSTNCE}:
"HASH(0x182f948)"..

$Policy->{INSTNCE}{$CondID}->{ACTIONS}:
"HASH(0x182f960)"..

$Policy->{INSTNCE}{$CondID}->{ACTIONS}{START}{SEVRTY}:
"SupperDupperHigh"..

$Policy->{INSTNCE}{$CondID}->{ACTIONS}{CONT}{TEXT}:
"Head for the hills"..

$Policy->{INSTNCE}{$CondID}->{ACTIONS}{END}{AUTACTS}:
"Shut her down"..
-------------------------------------------------

C:\Documents and Settings\eendh66\Desktop>
============================================================ ==========

Flk k


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message