Dear Monks,

I have a quite complicated object (for various reason it has to be like this) and by using this example I want to ask how to do to specific tasks. I hope it will be easy to understand.

#Created by using; push @{ $Second{Company}->{Identifier} }, Element;

%Hash -> {Company_A} -> { Identifier_1 } -> One or more 'Elements' e +xample (a, b) -> { Identifier_2 } -> One or more 'Elements' e +xample (b, g) -> { Identifier_3 } -> One or more 'Elements' e +xample (c) -> {Company_B} -> { Identifier_1 } -> One or more 'Elements' e +xample (a) -> { Identifier_2 } -> One or more 'Elements' e +xample (g) -> { Identifier_3 } -> One or more 'Elements' e +xample (x)

1) I want to know how many 'b' elements is there in Company_A. Usually I would use something like map { $Count+= 1 if 'a' eq values %Hash{Company_A}{$_} } values %Hash{Company_A}; but it will only access the first element of each Identifier. How can I write a map command to go through every element?

2) When I need to print this hash I have to look if each identifier has one or more elements (scalar) and fork the prints to print out everything in line by line case (foreach and use count++). Amount of elements vary and there is a lot of data inside so I don't want to create duplicate entries</pp>

scalar () if more than 1 { foreach $Company_A{Identifier_1}[$Count] $Company_A{Identifier_2}[$Count] $Company_A{Identifier_3} $Count++ } if = 1 { $Company_B{Identifier_1} $Company_B{Identifier_2} $Company_B{Identifier_3} } Ouptut: Company_A a, b, c Company_A b, g, c Company_B a, g, x

Some Identifiers have more elements (always equal amount) and other have only one. I need to print the ones with multiple elements separately - they share the elements which only appear once. Because I am doing it for separate objects I was wondering if there is a way to print everything in one block with a single command?

Thanks so much for any tips. Humble apprentice


In reply to Searching and printing complex data structures by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.