Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi,

I have a pretty complicated (for me anyway) hash setup as shown in the example snippet below:

my %hash = ( 'A1' => { 'B1' => 'Value', 'B2' => { 'C1' => [arrayitem1, arrayitem2, arrayitem3,], 'C2' => [arrayitem1, arrayitem2, arrayitem3,], } }, )

What I want to do is check whether C1 or C2 exists. If it exists, I want to add another item to its array. If C1 or C2 does not exist, I want to create it as an array reference and add an item to its array.

At the moment my code looks something like this:

if (exists $hash{'A1'}{'B2'}{'C1'}) { push ( @{ $hash{'A1'}{'B2'}{'C1'}}, arrayitem4; } else { $hash{'A1'}{'B2'} = {'C1' => [arrayitem1]}; }

The code doesn't work as I get various "Can't use string ("") as a HASH ref while "strict refs" in use" errors. I am getting very confused with all of the searching I have done to find the answer so would really appreciate it if someone could point me in the right direction.

The aim is that B2 would be a 'Test Results' key. The 'C' keys would refer to dates and then the arrays within them would be the data of the results (single line sentences) for that dates tests


In reply to How to add to hash of a hash of an array. Confused.com! by Doozer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found